每当我向声明为非空值的 MySQL 列中插入空字符串时,它为何显示 0 而不是空字符串?


这是因为插入空字符串意味着我们插入了一些值,而不是 NULL。空字符串显然映射为 0,表示为整数。换句话说,我们认为通过插入空字符串,我们为 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-02-14

1 千次以上浏览

开启你的 职业生涯

完成该课程以获得认证

开始
广告
© . All rights reserved.