將空字串插入宣告為 NOT NULL 的 MySQL 列時,資料型別起什麼作用?
將空字串插入宣告為 NOT NULL 的 MySQL 列時,結果集中的空字串表示取決於資料型別。眾所周知,插入空字串時,我們向 MySQL 提供的值的整數表示為 INT 0。
現在,如果該列的資料型別為 INTEGER,則 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)但是,如果該列具有任何其他資料型別,例如 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 列時,資料型別起到的作用。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP