时间:2021-07-01 10:21:17 帮助过:21人阅读
插入一条记录,name的值为null
在非严格模式下运行
mysql> insert into mytable(content) values(‘programmer‘);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from mytable;
+----+------+------------+
| id | name | content |
+----+------+------------+
| 1 | | programmer |
+----+------+------------+
1 row in set (0.00 sec)
运行成功,name的值自己主动转为”
在严格模式下运行
mysql> insert into mytable(content) values(‘programmer‘);
ERROR 1364 (HY000): Field ‘name‘ doesn‘t have a default value
运行失败,提示字段name不能为null值
对id字段插入”值
在非严格模式下运行
mysql> insert into mytable(id,name,content) value(‘‘,‘fdipzone‘,‘programmer‘);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from mytable;
+----+----------+------------+
| id | name | content |
+----+----------+------------+
| 1 | fdipzone | programmer |
+----+----------+------------+
1 row in set (0.00 sec)
运行成功
在严格模式下运行
mysql> insert into mytable(id,name,content) value(‘‘,‘fdipzone‘,‘programmer‘);
ERROR 1366 (HY000): Incorrect integer value: ‘‘ for column ‘id‘ at row 1
运行失败。提示字段id不能为”
mysql> insert into mytable(id,name,content) value(null,‘fdipzone‘,‘programmer‘);
Query OK, 1 row affected (0.00 sec)
mysql> select * from mytable;
+----+----------+------------+
| id | name | content |
+----+----------+------------+
| 1 | fdipzone | programmer |
+----+----------+------------+
1 row in set (0.00 sec)
字段id为null则可运行成功
创建一个数据表mytable。当中text设置默认值default=”
在非严格模式下运行
mysql> CREATE TABLE `mytable` (
-> `id` int(11) NOT NULL AUTO_INCREMENT,
-> `name` varchar(20) NOT NULL,
-> `content` text NOT NULL default ‘‘,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> show tables;
+------------------------------+
| Tables_in_test_version |
+------------------------------+
| mytable |
+------------------------------+
运行成功
在严格模式下运行
mysql> CREATE TABLE `mytable` (
-> `id` int(11) NOT NULL AUTO_INCREMENT,
-> `name` varchar(20) NOT NULL,
-> `content` text NOT NULL default ‘‘,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ERROR 1101 (42000): BLOB/TEXT column ‘content‘ can‘t have a default value
运行失败,提示content字段是TEXT类型,不能使用默认值。
总结。使用mysql严格模式能够使数据更加安全严格,缺点是降低了对空数据入库的兼容性。建议开发环境使用严格模式以提高代码的质量及对数据的严谨性。
mysql 严格模式 Strict Mode说明
标签:nes show erro err correct 没有 ascii engine 数据表