gpt4 book ai didi

mysql - 为什么我可以将 null 插入到 mysql 字段中,它是 varchar 而不是 null

转载 作者:可可西里 更新时间:2023-11-01 07:05:01 25 4
gpt4 key购买 nike

我在mysql中创建了一个表

create table test (id int primary key not null auto_increment, vs varchar(255) not null);

运行时

insert into test (vs) values (null);

它抛出一个错误:

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

但是当我尝试通过

插入两行时
insert into test (vs) values (null),(null);

它有效,结果是:

mysql> select * from test;
+----+----+
| id | vs |
+----+----+
| 1 | |
| 2 | |
+----+----+

字段vs不可为空,不知是不是一个特性。

最佳答案

这既不是错误也不是功能。这就是它在这种情况下的工作方式。此行为记录在 MySQL documentation - Constraints on Invalid Data 中.

If you try to store NULL into a column that doesn't take NULL values, an error occurs for single-row INSERT statements. For multiple-row INSERT statements or for INSERT INTO ... SELECT statements, MySQL Server stores the implicit default value for the column data type. In general, this is 0 for numeric types, the empty string ('') for string types, and the “zero” value for date and time types. Implicit default values are discussed in Section 11.7, “Data Type Default Values”.

当您关闭严格模式时会发生这种情况。

关于mysql - 为什么我可以将 null 插入到 mysql 字段中,它是 varchar 而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46112971/

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