pow*_*tac 1 mysql insert errors warning mysql-5.6
CREATE TABLE `dummy` (
`a` TINYINT(4) NOT NULL
) ENGINE=MyISAM;
Run Code Online (Sandbox Code Playgroud)
1 错误值
INSERT INTO `dummy` (`a`) VALUES (NULL);
/* SQL Fehler (1048): Column 'a' cannot be null */
/* Nothing is stored! */
Run Code Online (Sandbox Code Playgroud)
2个错误的值
INSERT INTO `dummy` (`a`) VALUES (NULL), (NULL);
/* Affected rows: 2 Gefundene Zeilen: 0 Warnungen: 2 Dauer von Abfrage: ... sec. */
SHOW WARNINGS LIMIT 5; /* Not sure where this comes from! Maybe HeidiSQL? */
/* Two entries are stored! */
Run Code Online (Sandbox Code Playgroud)
为什么 MySQL 会在单值查询中引发错误,而在具有两个值的第二个查询中却没有?
它与SHOW WARNINGS LIMIT 5;
但这条线来自哪里以及为什么将错误类型从错误更改为警告有关?
MySQL V5.6.31
这是 MySQL 的设计方式。根据文档:
如果您尝试将 NULL 存储到不采用 NULL 值的列中,则单行 INSERT 语句会发生错误。对于多行 INSERT 语句或 INSERT INTO ... SELECT 语句,MySQL Server 存储列数据类型的隐式默认值。通常,数字类型为 0,字符串类型为空字符串 (''),日期和时间类型为“零”值。....
我做了一个测试来查看表的内容(注意我收到了警告!):
mysql> CREATE TABLE `dummy` (
-> `a` TINYINT(4) NOT NULL
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO `dummy` (`a`) VALUES (NULL);
ERROR 1048 (23000): Column 'a' cannot be null
mysql> INSERT INTO `dummy` (`a`) VALUES (NULL), (NULL);
Query OK, 2 rows affected, 2 warnings (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 2
mysql> show warnings;
+---------+------+---------------------------+
| Level | Code | Message |
+---------+------+---------------------------+
| Warning | 1048 | Column 'a' cannot be null |
| Warning | 1048 | Column 'a' cannot be null |
+---------+------+---------------------------+
2 rows in set (0.00 sec)
mysql> select * from dummy;
+---+
| a |
+---+
| 0 |
| 0 |
+---+
2 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
799 次 |
最近记录: |