MySQL 即使在必填字段不存在时也允许插入
MySQL allows inserts even when required fields are not present
我有一个数据库 table 供具有与此类似设置的用户使用:
+---------------------+--------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+------------+----------------+
| id_user | int(5) | NO | PRI | NULL | auto_increment |
| login | varchar(50) | NO | | | |
| password | varchar(50) | NO | | | |
| address | varchar(255) | NO | | NULL | |
+---------------------+--------------+------+-----+------------+----------------+
由于地址字段设置为非空并且默认为空,这应该有效地使地址字段成为必需的,对吧?
但是当我执行如下查询时:
INSERT INTO mydatabase.users
SET
mydatabase.users.login='test_username',
mydatabase.users.password='test_password'
它成功地拯救了用户,没有抱怨地址字段被设置为空space。
这可能是什么原因造成的?我预计在这种情况下会出现错误。我用 MySQL 5.5 和 5.6 试过了。
Strict SQL mode在配置文件中没有启用,因此mysql放宽了对数据插入的某些控制:
Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.) Strict mode also affects DDL statements such as CREATE TABLE.
我有一个数据库 table 供具有与此类似设置的用户使用:
+---------------------+--------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+------------+----------------+
| id_user | int(5) | NO | PRI | NULL | auto_increment |
| login | varchar(50) | NO | | | |
| password | varchar(50) | NO | | | |
| address | varchar(255) | NO | | NULL | |
+---------------------+--------------+------+-----+------------+----------------+
由于地址字段设置为非空并且默认为空,这应该有效地使地址字段成为必需的,对吧?
但是当我执行如下查询时:
INSERT INTO mydatabase.users
SET
mydatabase.users.login='test_username',
mydatabase.users.password='test_password'
它成功地拯救了用户,没有抱怨地址字段被设置为空space。
这可能是什么原因造成的?我预计在这种情况下会出现错误。我用 MySQL 5.5 和 5.6 试过了。
Strict SQL mode在配置文件中没有启用,因此mysql放宽了对数据插入的某些控制:
Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.) Strict mode also affects DDL statements such as CREATE TABLE.