
当我们将空字符串插入声明为 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 列时,数据类型扮演什么角色。
以上就是当我将空字符串插入声明为 NOT NULL 的 MySQL 列时,数据类型起什么作用?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号