欢迎光临散文网 会员登陆 & 注册

MySQL数据库的表中 NULL 和 空值 到底有什么区别呢?

2020-09-14 18:55 作者:极客小俊GeekerJun  | 我要投稿

极客小俊

一个专注于web技术的80后

你不用拼过聪明人,你只需要拼过那些懒人 你就一定会超越大部分人!



我们来浅谈一下MySQL中的 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查询,而空值('')可以使用=或者!=、<、>等算术运算符来查!


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值还是空值('')呢?

根据实际业务来进行区分, 个人建议在实际开发中如果没有特殊的业务场景,可以直接使用空字符串值('') !


如果喜欢话请 点赞  投币  收藏 一键三连  

大家的支持就是我坚持下去的动力!

不要忘了👉 关注 👈哦!




MySQL数据库的表中 NULL 和 空值 到底有什么区别呢?的评论 (共 条)

分享到微博请遵守国家法律