sql - 在 SQL 中插入不应该包含 null 的记录

标签 sql mysql insert

我正在为一个简单的站点编写 SQL 数据库,但有一个问题。 我创建了一个表“test”,其中包含 int test1 NOT NULLint 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/

相关文章:

php - 在页面加载时插入查询,插入两次?

sql - 选择时如何忽略某些相似的行

mysql - 已在列中时字段列表中的未知列

java - 通过jdbc访问主机时如何克服词法错误?

mysql - SQL 查询和结果

c# - ADO.NET 适当的数据插入

sql - 如何选择与 PostgreSQL 值列表中匹配的字符串的第一部分?

mysql - MySQL如何比较两个日期

MySQL 返回引用同一个表的行

c# - 插入带有 Dapper 的 IEnumerable<T> 集合时出现 "class is not supported by Dapper."错误