平時咱們在使用MySQL的時候,對於MySQL中的NULL值和空值區別不能很好的理解。注意到NULL值是未知的,且佔用空間,不走索引,DBA建議建表的時候最好設置字段是NOT NULL 來避免這種低效率的事情的發生。
mysql> show create table testaa;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| testaa | CREATE TABLE `testaa` (
`a` int(11) NOT NULL,
`b` varchar(20) DEFAULT NULL,
`c` varchar(20) NOT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
插入測試數據:
mysql> insert testaa values (1,'aa','');
Query OK, 1 row affected (0.00 sec)
mysql> insert testaa values (2,'','');
Query OK, 1 row affected (0.00 sec)
mysql> insert testaa values (3,null,'');
Query OK, 1 row affected (0.00 sec)
mysql> insert testaa values (4,NULL,'');
Query OK, 1 row affected (0.00 sec)
mysql> insert testaa values (5,'aafa','fa');
Query OK, 1 row affected (0.00 sec)
mysql> insert testaa values (6,'',NULL);
ERROR 1048 (23000): Column 'c' cannot be null
mysql> select * from testaa;
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | aa | |
| 2 | | |
| 3 | NULL | |
| 4 | NULL | |
| 5 | aafa | fa |
+---+------+----+
查詢驗證過程:
mysql> select * from testaa where c is not null;
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | aa | |
| 2 | | |
| 3 | NULL | |
| 4 | NULL | |
| 5 | aafa | fa |
+---+------+----+
5 rows in set (0.00 sec)
mysql> select * from testaa where c <> '';
+---+------+----+
| a | b | c |
+---+------+----+
| 5 | aafa | fa |
+---+------+----+
1 row in set (0.00 sec)
mysql> select * from testaa where c = '';
+---+------+---+
| a | b | c |
+---+------+---+
| 1 | aa | |
| 2 | | |
| 3 | NULL | |
| 4 | NULL | |
+---+------+---+
4 rows in set (0.00 sec)
mysql> select * from testaa where c is null;
Empty set (0.00 sec)
mysql> select * from testaa where b is not null;
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | aa | |
| 2 | | |
| 5 | aafa | fa |
+---+------+----+
3 rows in set (0.00 sec)
mysql> select * from testaa where b <> '';
+---+------+----+
| a | b | c |
+---+------+----+
| 1 | aa | |
| 5 | aafa | fa |
+---+------+----+
2 rows in set (0.00 sec)
mysql> select * from testaa where b ='';
+---+------+---+
| a | b | c |
+---+------+---+
| 2 | | |
+---+------+---+
1 row in set (0.00 sec)
mysql> select * from testaa where b is null;
+---+------+---+
| a | b | c |
+---+------+---+
| 3 | NULL | |
| 4 | NULL | |
+---+------+---+
mysql> select length(b),length(c) from testaa;
+-----------+-----------+
| length(b) | length(c) |
+-----------+-----------+
| 2 | 0 |
| 0 | 0 |
| NULL | 0 |
| NULL | 0 |
| 4 | 2 |
+-----------+-----------+
5 rows in set (0.00 sec)
mysql> select count(b),count(c) from testaa;
+----------+----------+
| count(b) | count(c) |
+----------+----------+
| 3 | 5 |
+----------+----------+
1 row in set (0.00 sec)
mysql> create table testbb ( a int primary key , b timestamp);
Query OK, 0 rows affected (0.07 sec)
mysql> show create table testbb;
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| testbb | CREATE TABLE `testbb` (
`a` int(11) NOT NULL,
`b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
mysql> insert into testbb vales (1,null) ;
mysql> insert into testbb values (2,'');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+----------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'b' at row 1 |
+---------+------+----------------------------------------+
mysql> select * from testbb;
+---+---------------------+
| a | b |
+---+---------------------+
| 1 | 2014-08-15 14:32:10 |
| 2 | 0000-00-00 00:00:00 |
+---+---------------------+
2 rows in set (0.00 sec)
注意事項:
1:在進行count()統計某列的記錄數的時候,若是採用的NULL值,會別系統自動忽略掉,可是空值是會進行統計到其中的。
2: 判斷NULL 用IS NULL 或者 is not null,SQL 語句函數中能夠使用ifnull()函數來進行處理,判斷空字符用 =''或者 <>''來進行處理
3: 對於MySQL特殊的注意事項,對於timestamp數據類型,若是往這個數據類型插入的列插入NULL值,則出現的值是當前系統時間。插入空值,則會出現 '0000-00-00 00:00:00'
4:對於空值的判斷究竟是使用is null 仍是 =''要根據實際業務來進行區分。