当我们将空字符串插入声明为 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: 1mysql> 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: 1mysql> select * from test123;+----+--------+| id | name |+----+--------+| 1 | gaurav || 0 | rahul || | aarav |+----+--------+3 rows in set (0.00 sec)
从上面的例子中,我们可以看到当我们将一个空字符串插入声明为 not null 的 mysql 列时,数据类型扮演什么角色。
以上就是当我将空字符串插入声明为 not null 的 mysql 列时,数据类型起什么作用?的详细内容。
