是否有任何配置设置可以阻止MySQL为没有默认值且未在insert语句中指定的字段插入空值? 我更倾向于拒绝无效的插入声明。
示例 - 我在MySQL中有以下表格:
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`val1` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`val2` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
如果运行此insert语句:
INSERT INTO test (val2) VALUES('a');
您收到此回复:
Query OK, 1 row affected, 1 warning (0.00 sec)
来自show warnings
的回复:
+---------+------+-------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------+
| Warning | 1364 | Field 'val1' doesn't have a default value |
+---------+------+-------------------------------------------+
1 row in set (0.00 sec)
来自SELECT * FROM test
的回复:
+----+------+------+
| id | val1 | val2 |
+----+------+------+
| 1 | | asdf |
+----+------+------+
1 row in set (0.00 sec)