首先,我们先看看test表的所有内容:
mysql> select * from test;
+----+-------+------+-------------+
| id | name | age | phone |
+----+-------+------+-------------+
| 1 | li | 23 | |
| 2 | zhang | NULL | |
| 3 | wu | NULL | 12345678912 |
| 4 | wang | NULL | |
+----+-------+------+-------------+
查询phone不为NULL的用户有:
mysql> select * from test where phone is not null;
+----+-------+------+-------------+
| id | name | age | phone |
+----+-------+------+-------------+
| 1 | li | 23 | |
| 2 | zhang | NULL | |
| 3 | wu | NULL | 12345678912 |
| 4 | wang | NULL | |
+----+-------+------+-------------+
查找phone为空值的用户有:
mysql> select * from test where phone ="";
+----+-------+------+-------+
| id | name | age | phone |
+----+-------+------+-------+
| 1 | li | 23 | |
| 2 | zhang | NULL | |
+----+-------+------+-------+
可见NULL与空值是不同的:
1、空值不占用空间,相当于真空的杯子;
2、NULL仍占用空间,相当于空杯,但里面有空气,下面是来自于MYSQL官方的解释
“NULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.”
在实际工作中,NULL与空值往往表示不同的含义,应该根据实际的需要来进行选择。如对于电话号码等字段,可以默认设置为NULL(表示根本不知道对方的电话号码)或者设置为空值(表示后来取消了这个号码)等等。