gpt4 book ai didi

即使不存在必需字段,MySQL 也允许插入

转载 作者:行者123 更新时间:2023-11-29 05:14:39 26 4
gpt4 key购买 nike

我有一个用户的数据库表,其设置与此类似:

+---------------------+--------------+------+-----+------------+----------------+
| 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'

它成功地拯救了用户,没有提示地址字段被设置为空白。

这可能是什么原因造成的?我预计在这种情况下会出现错误。我已经在 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.

关于即使不存在必需字段,MySQL 也允许插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34766666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com