当我往一个声明为 NOT NULL 的 MySQL 列中插入一个空字符串时,为什么它会显示 0 而不是空字符串?


这是因为插入空字符串意味着我们插入的是某个值而不是 NULL。显然,空字符串映射到零,作为一个整数。换句话说,我们可以说,通过插入空字符串,我们向 MySQL 提供的值的整数表示为 INT 0。考虑以下示例,我们在其中插入了一个空字符串,MySQL 将其映射到 0。

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)

更新时间: 2020 年 2 月 14 日

1 千 + 次浏览

职业起航

完成课程,获得认证

入门
广告
© . All rights reserved.