https://blog.csdn.net/yu757371316/article/details/53033118
https://blog.csdn.net/u014743697/article/details/54136092/
https://blog.csdn.net/qq_15096707/article/details/51541274
https://www.cnblogs.com/wangzhongqiu/p/6424712.html
mysql> create table if not exists `test`(`a` varchar(10) not null,`b` varchar(10) null);
Query OK, 0 rows affected (0.56 sec)
mysql> desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| a | varchar(10) | NO | | NULL | |
| b | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.12 sec)
mysql> insert into `test` values (null,'1');
1048 - Column 'a' cannot be null //a设置为Not Null后无法插入Null值,但可插入'' 。
mysql> insert into `test`(`a`,`b`) values (1,null);
Query OK, 1 row affected (0.07 sec)
mysql> select*from `test`;
+---+------+
| a | b |
+---+------+
| 1 | NULL |
+---+------+
1 row in set (0.03 sec)
mysql> insert into `test` values('','');//空字符可正常插入。
Query OK, 1 row affected (0.07 sec)
mysql> select*from `test`;
+---+------+
| a | b |
+---+------+
| 1 | NULL |
| | |
+---+------+
2 rows in set (0.04 sec)
mysql> insert into `test` values('',null);
Query OK, 1 row affected (0.03 sec)
mysql> select*from `test`;
+---+------+
| a | b |
+---+------+
| 1 | NULL |
| | |
| | NULL |
+---+------+
3 rows in set (0.05 sec)
mysql> insert into `test` values(1,2);
Query OK, 1 row affected (0.04 sec)
mysql> select*from `test`;
+---+------+
| a | b |
+---+------+
| 1 | NULL |
| | |
| | NULL |
| 1 | 2 |
+---+------+
4 rows in set (0.03 sec)
mysql> select*from `test` where `a` is not null; //证明''与 Not Null是相对比较独立的两个东西。
+---+------+
| a | b |
+---+------+
| 1 | NULL |
| | |
| | NULL |
| 1 | 2 |
+---+------+
4 rows in set (0.05 sec)
mysql> select*from `test` where `b` is not null;
+---+---+
| a | b |
+---+---+
| | |
| 1 | 2 |
+---+---+
2 rows in set (0.04 sec)
mysql> select*from `test` where `a`<>'';
+---+------+
| a | b |
+---+------+
| 1 | NULL |
| 1 | 2 |
+---+------+
2 rows in set (0.06 sec)
mysql> select * from `test` where `b`<>''; //<>''非空字符会将Null排除在外,此处比较奇怪。
+---+---+
| a | b |
+---+---+
| 1 | 2 |
+---+---+
1 row in set (0.03 sec)
mysql> select*from `test` where `b`=''; //选择''时,Null不算在此列,相对独立。
+---+---+
| a | b |
+---+---+
| | |
+---+---+
1 row in set (0.05 sec)
上述命令,大致可以看到 '' 、<>''与Null和Not Null几乎可以说是相对独立的两个东西,但是有一处比较奇怪,需要注意<>'' 会把Null排除在外,这点又会使人觉得''就是Null,具体为何,还是不知道。
另外一点奇怪的是,我用的参考文章的方法和命令,选出的结果却是不同的,不知道是不是mysql版本不同造成的,我的版本是免安装5.7.22。