将空字符串插入声明为 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