示例
mysql> create table test(id int NOT NULL, Name Varchar(10)); Query OK, 0 rows affected (0.19 sec) mysql> Insert into test(id, name) values('1', 'Gaurav'),('0','Rahul'),('','Aarav'); Query OK, 3 rows affected, 1 warning (0.08 sec) Records: 3 Duplicates: 0 Warnings: 1 mysql> Select * from test; +----+--------+ | id | Name | +----+--------+ | 1 | Gaurav | | 0 | Rahul | | 0 | Aarav | +----+--------+ 3 rows in set (0.00 sec)
但是如果该列具有任何其他数据类型(例如 VARCHAR),那么 MySQL 将在结果集中显示空字符串。
mysql> create table test123(id Varchar(10) NOT NULL, Name Varchar(10)); Query OK, 0 rows affected (0.19 sec) mysql> Insert into test123(id, name) values('1', 'Gaurav'),('0','Rahul'),('','Aarav'); Query OK, 3 rows affected, 1 warning (0.08 sec) Records: 3 Duplicates: 0 Warnings: 1 mysql> Select * from test123; +----+--------+ | id | Name | +----+--------+ | 1 | Gaurav | | 0 | Rahul | | | Aarav | +----+--------+ 3 rows in set (0.00 sec)
从上面的例子中,我们可以看到当我们将一个空字符串插入声明为 NOT NULL 的 MySQL 列时,数据类型扮演什么角色。