我正在为一个简单的站点编写 SQL 数据库,但有一个问题。
我创建了一个表“test”,其中包含 int test1 NOT NULL
和 int test2 NOT NULL
。无默认值。
当我写作时
INSERT INTO test
(test1, test2)
VALUES
(1,2)
...一切都很好。
当我使用时
INSERT INTO test
(test1)
VALUES
(4)
它不应该抛出错误吗?相反,它添加了一条 test1 = 4 和 test2 = 0 的记录。
这是怎么回事?
最佳答案
以下内容在版本 5.1.42 上按预期工作。 你用的是什么版本?
create table test(
test1 int not null
,test2 int not null
);
Query OK, 0 rows affected (0.05 sec)
insert into test(test1, test2) values(1,2);
Query OK, 1 row affected (0.00 sec)
insert into test(test1) values(4);
ERROR 1364 (HY000): Field 'test2' doesn't have a default value
insert into test(test2) values(4);
ERROR 1364 (HY000): Field 'test1' doesn't have a default value
select * from test;
+-------+-------+
| test1 | test2 |
+-------+-------+
| 1 | 2 |
+-------+-------+
1 row in set (0.00 sec)
关于sql - 在 SQL 中插入不应该包含 null 的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4381075/