當我往一個宣告為 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.