tft每日頭條

 > 科技

 > mysql 為null是什麼值

mysql 為null是什麼值

科技 更新时间:2024-10-13 05:45:38
淺談 NULL 和 空值的區别

NULL也就是在字段中存儲NULL值

空字符串值也就是字段中存儲空字符('')

我們來通過測試來看看 他們彼此的區别:

1、占用空間區别

mysql> select length(NULL), length(''), length('1'); -------------- ------------ ------------- | length(NULL) | length('') | length('1') | -------------- ------------ ------------- | NULL | 0 | 1 | -------------- ------------ ------------- 1 row in set (0.03 sec) 複制代碼

==小結== : 從上面的測試可以看出 字符串空值('')的長度是0,是不占用空間的, 而的NULL長度是NULL,其實它是占用空間的!

NULL columns require additional space in the row to record whether their values are NULL.

意思是: NULL列需要行中的額外空間來記錄它們的值是否為NULL

通俗意義上講: ('')字符串空值就像是一個真空轉态杯子,什麼都沒有,而NULL值就是一個裝滿空氣的杯子,雖然看起來都是一樣的,但是有着本質的區别

2、插入方式區别

#創建一個表,tb_test create table tb_test( id int unsigned primary key auto_increment, one varchar(10) NOT NULL, two varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 插入進行驗證: #全部插入 NULL,會失敗 原因就是指定的不允許插入NULL insert into tb_test(one,two) value (NULL,NULL); 1048 - Column 'one' cannot be null #全部插入 空字符串值,成功 原因就是 ('') 字符 和 NULL的類型都不一樣 指定的是不允許插入NULL,又沒有說不允許('')空字符串!^.^ insert into tb_test(one,two) value ('',''); Query OK, 1 row affected #這也是剛剛講過not null約束測試insert語句的時候, 插入('')空字符串會成功的原因! 複制代碼

3、在查詢方式上的區别對比

#創建一個表,tb_test2 create table tb_test2( id int unsigned primary key auto_increment, one varchar(10) NOT NULL, two varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; #模拟數據: insert into tb_test2(one,two) values (1,NULL); insert into tb_test2(one,two) values ('',2); insert into tb_test2(one,two) values (3,3); #查詢one字段 #使用 is null 來查詢one字段 select * FROM tb_test2 where one is null; #結果就是一條也沒有,因為one字段并沒有代表為NULL的數據存在! #使用 is not null 來查詢one字段 select * FROM tb_test2 where one is not null; #結果被全部查詢出來,因為one字段中的三個數據都不為NULL這個類型 #使用 = 和 != 來查詢one字段 select * FROM tb_test2 where one =''; select * FROM tb_test2 where one != ''; #查詢two字段 #使用 is null 來查詢two字段 select * FROM tb_test2 where two is null; #結果有一條符合NULL, #使用 is not null 來查詢two字段 select * FROM tb_test2 where two is not null; #結果是不符合NULL的有兩條 #使用 = 來查詢two字段 select * FROM tb_test2 where two =''; #使用 != 來查詢two字段 #這裡要注意的是為NULL的并沒有查詢出來,原因用 != 來查 字符串空('')的時候, 會把NULL也當做是字符串空來判斷吧! select * FROM tb_test2 where two != ''; 複制代碼

==小結:== 如果要單純查NULL值列,則使用 is NULL去查,單純去查空值('')列,則使用 =''。

建議查詢方式:NULL值查詢使用is null/is not null查詢,而空值('')可以使用=或者!=、<、>等算術運算符來查!

mysql 為null是什麼值(MySQL數據庫的表中NULL和)1

4、在count()統計函數上的區别

#創建一個表,tb_test3 create table tb_test3( id int unsigned primary key auto_increment, one varchar(10) NOT NULL, two varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; #模拟數據: insert into tb_test3(one,two) values (1,NULL); insert into tb_test3(one,two) values ('',2); insert into tb_test3(one,two) values (3,3); #使用COUNT函數統計one字段: select count(one) from tb_test3; #結果為: 3 條, 說明 空字符串('') 會被count()函數統計! #使用COUNT函數統計two字段: select count(two) from tb_test3; #結果為: 2條, 原因是NULL 不會被count()函數統計到! #注意: 使用 * 号來統計會把NULL算進去! SELECT count(*) FROM tb_test; ---------- | count(*) | ---------- | 3 | ---------- 複制代碼

實際開發到底是使用NULL值還是空值('')呢?

根據實際業務來進行區分, 個人建議在實際開發中如果沒有特殊的業務場景,可以直接使用空字符串值('') !

作者:極客小俊GeekerJun鍊接:https://juejin.im/post/6871709918139777037

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved