每當我向宣告為非空值的 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.