在mysql數(shù)據(jù)庫(kù)中,null是一個(gè)經(jīng)常出現(xiàn)的情況,關(guān)于mysql中的null,有哪些注意事項(xiàng)呢?下面簡(jiǎn)單總結(jié)歸納下,后續(xù)會(huì)不斷補(bǔ)充。
1. is null
首先判斷數(shù)據(jù)庫(kù)中某一列的值是否為null,不能用等于來(lái)判斷,必須使用 is。比如,select * from users where user_name is null 或者 select * from users where user_name is not null,而不能是 select * from users where user_name = null
2. ISNULL( )
MySQL數(shù)據(jù)庫(kù)中內(nèi)置了 ISNULL( ) 函數(shù),其使用方法與MySQL中提供的sum()等其他內(nèi)置函數(shù)一樣。比如,select ISNULL(user_name) from users where user_name = ‘Demrystv' 返回值是 0;select ISNULL(NULL) 返回值是1
3. IFNULL()
MySQL數(shù)據(jù)庫(kù)中內(nèi)置了 IFNULL( ) 函數(shù),其使用方法與MySQL中提供的sum()等其他內(nèi)置函數(shù)一樣。其主要接收兩個(gè)參數(shù),第一個(gè)參數(shù)是要判斷空值的字段或值,第二個(gè)字段是當(dāng)?shù)谝粋€(gè)參數(shù)是空值的情況下要替換返回的另一個(gè)值,即如果第一個(gè)字段為null,會(huì)將其替換成其他值。比如,select IFNULL(NULL, “java is the best language of the world”),由于第一個(gè)參數(shù)的值NULL,因此會(huì)輸出第二個(gè)參數(shù)的值,java is the best language of the world;同理,若第一個(gè)字段不為空,則會(huì)返回第一個(gè)字段的值。
4. insert into 與null
在使用insert into往表中灌數(shù)據(jù)的時(shí)候,需要首先明確一下表里是null,還是空,如果是null,那么就不能使用 insert into進(jìn)行灌數(shù)據(jù),必須使用 update,這一點(diǎn)看似簡(jiǎn)單,但是在實(shí)際開(kāi)發(fā)中經(jīng)常容易忽略,因此需要特殊注意。
例子:
創(chuàng)建一個(gè)test表,colA是不可以存放null值的,colB是能存放null值的。
CREATE TABLE `test` (
`colA` varchar(255) NOT NULL,
`colB` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
以上就是本次介紹的全部相關(guān)知識(shí)點(diǎn),感謝大家的學(xué)習(xí)和對(duì)腳本之家的支持。
您可能感興趣的文章:- php基礎(chǔ)之連接mysql數(shù)據(jù)庫(kù)和查詢數(shù)據(jù)
- PHP連接和操作MySQL數(shù)據(jù)庫(kù)基礎(chǔ)教程
- Mysql入門基礎(chǔ) 數(shù)據(jù)庫(kù)創(chuàng)建篇
- mysql 8.0.15 安裝圖文教程及數(shù)據(jù)庫(kù)基礎(chǔ)
- MySQL數(shù)據(jù)庫(kù)基礎(chǔ)命令大全(收藏)
- 很全面的Mysql數(shù)據(jù)庫(kù)、數(shù)據(jù)庫(kù)表、數(shù)據(jù)基礎(chǔ)操作筆記(含代碼)
- mysql 數(shù)據(jù)庫(kù)基礎(chǔ)筆記
- MySQL數(shù)據(jù)庫(kù)的實(shí)時(shí)備份知識(shí)點(diǎn)詳解
- nodejs連接mysql數(shù)據(jù)庫(kù)及基本知識(shí)點(diǎn)詳解
- mysql數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)點(diǎn)與操作小結(jié)