mysql设置字段为空字符串_NULL默认为mysql中的空字符串?

我有一个奇怪的情况,一个字段不允许NULL值.如果我插入一行,该字段默认为空字符串,即使mysql声称默认值为NULL.

mysql> describe user;

+---------------------------+------------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+---------------------------+------------------+------+-----+---------+----------------+

| id | int(30) | NO | PRI | NULL | auto_increment |

| username | varchar(45) | NO | UNI | NULL | |

| city | varchar(45) | NO | | NULL | |

+---------------------------+------------------+------+-----+---------+----------------+

mysql> show triggers;

Empty set (0.00 sec)

mysql> insert into user (username) values ('just_testing');

Query OK, 1 row affected, 17 warnings (0.01 sec)

这是我去哪里的地方?! – city应该默认为NULL,这是不允许的,但请看这里:

mysql> select username, city from user where username = 'just_testing' and city is null;

Empty set (0.00 sec)

mysql> select username, city from user where username = 'just_testing' and city='';

+--------------+------+

| username | city |

+--------------+------+

| just_testing | |

+--------------+------+

1 row in set (0.00 sec)

Mysql决定使用空字符串作为默认值,即使它不是这样,并且没有任何触发器.

并进一步:

mysql> insert into user (username, city) values ('just_testing3', NULL);

ERROR 1048 (23000): Column 'city' cannot be null

我在俯瞰什么?城市列如何默认为”?

解决方法:

If you are not running in strict SQL mode, any column not explicitly given a value is set to its default (explicit or implicit) value. For example, if you specify a column list that does not name all the columns in the table, unnamed columns are set to their default values. Default value assignment is described in Section 11.6, “Data Type Default Values”. See also Section 1.8.3.3, “Constraints on Invalid Data”.

For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.

标签:mysql,sql

来源: https://codeday.me/bug/20190528/1169952.html


版权声明:本文为weixin_35032861原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。