前段时间升级MySQL到了5.6,但是后来app 后台日志显示一些SQL insert 出错,手动执行提示有些字段没有default值,查看了表语法该字段是Not null,而且升级也是OS copy数据库文件(因为是MyISAM 引擎),对比升级前后表结构一致,正常对于not null 的列并且没有default的值,当在insert 时 而在值列表未指定值理论就是应该不通过的,只能说是5.6以前 忽略了not null 的约束。
下面我演示一下。
Tip:
看到5.5是可以insert 只是 MySQL自动给not null的值加了default null。下面看一下5.6
后来google到了一些案例
如
Inserting NULLs into NOT NULL columns in 5.6: refused by default
MySQL ignores the NOT NULL constraint
The default SQL mode in MySQL 5.6.6 and later is NO_ENGINE_SUBSTITUTION; in MySQL 5.6.5 and earlier, it was empty (no modes set).
To change the SQL mode at runtime, set the global or session
于是参考上面的提示,修改SQL_MODE ,回退到以前的处理方式,因为程序程序没有人来修改原因。下面修改MySQL5.6
下面我演示一下。
mysql> select version();
+------------+
| version() |
+------------+
| 5.5.10-log |
+------------+
1 row in set (0.00 sec)
mysql> create table t(id int primary key not null auto_increment, name varchar(20) not null, addr varchar(10)) ENGINE=MyISAM ;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t(addr) values('bj');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from t;
+----+------+------+
| id | name | addr |
+----+------+------+
| 1 | | bj |
+----+------+------+
1 row in set (0.00 sec)
mysql> SELECT @@SESSION.sql_mode;
+--------------------+
| @@SESSION.sql_mode |
+--------------------+
| |
+--------------------+
1 row in set (0.00 sec)复制
Tip:
看到5.5是可以insert 只是 MySQL自动给not null的值加了default null。下面看一下5.6
mysql> select version();
+-------------------------------------------+
| version() |
+-------------------------------------------+
| 5.6.16-enterprise-commercial-advanced-log |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> create table t(id int primary key not null auto_increment, name varchar(20) not null, addr varchar(10)) ENGINE=MyISAM ;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t(addr) values('bj');
ERROR 1364 (HY000): Field 'name' doesn't have a default value
mysql> SELECT @@SESSION.sql_mode;
+--------------------------------------------+
| @@SESSION.sql_mode |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)复制
后来google到了一些案例
如
Inserting NULLs into NOT NULL columns in 5.6: refused by default
MySQL ignores the NOT NULL constraint
"As of MySQL 5.0.2, if a column definition includes no explicit DEFAULT value, MySQL determines the default value as follows:
If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause. This is the same as before 5.0.2.
If the column cannot take NULL as the value, MySQL defines the column with no explicit DEFAULT clause. For data entry, if an INSERT or REPLACE statement includes no value for the column, or an UPDATE statement sets the column to NULL, MySQL handles the column according to the SQL mode in effect at the time:
If strict SQL mode is not enabled, MySQL sets the column to the implicit default value for the column data type.
If strict mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an error occurs, but if this happens for the second or subsequent row of a multiple-row statement, the preceding rows will have been inserted."
The default SQL mode in MySQL 5.6.6 and later is NO_ENGINE_SUBSTITUTION; in MySQL 5.6.5 and earlier, it was empty (no modes set).
To change the SQL mode at runtime, set the global or session
sql_mode
system variable using a SET
statement:SET GLOBAL sql_mode = 'modes
';
SET SESSION sql_mode = 'modes
';复制
于是参考上面的提示,修改SQL_MODE ,回退到以前的处理方式,因为程序程序没有人来修改原因。下面修改MySQL5.6
mysql> SELECT @@SESSION.sql_mode;
+--------------------------------------------+
| @@SESSION.sql_mode |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)
mysql> SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@SESSION.sql_mode;
+------------------------+
| @@SESSION.sql_mode |
+------------------------+
| NO_ENGINE_SUBSTITUTION |
+------------------------+
1 row in set (0.00 sec)
mysql> insert into t(addr) values('bj');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from t;
+----+------+------+
| id | name | addr |
+----+------+------+
| 1 | | bj |
+----+------+------+
1 row in set (0.00 sec)
mysql> SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
9.9 分高危漏洞,尽快升级到 pgAdmin 4 v9.2 进行修复
严少安
335次阅读
2025-04-11 10:43:23
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
322次阅读
2025-04-15 14:48:05
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
281次阅读
2025-04-07 12:14:29
外国CTO也感兴趣的开源数据库项目——openHalo
小满未满、
208次阅读
2025-04-21 16:58:09
postgresql+patroni+etcd高可用安装
necessary
151次阅读
2025-03-28 10:11:23
从 Oracle 到 PostgreSQL迁移成本评估揭秘
梧桐
145次阅读
2025-03-27 17:21:42
手把手教你在 openKylin 上部署 IvorySQL 4.4
严少安
144次阅读
2025-03-27 20:41:28
转发有奖 | PostgreSQL 16 PGCM高级认证课程直播班招生中!
墨天轮小教习
141次阅读
2025-04-14 15:58:34
墨天轮PostgreSQL认证证书快递已发(2025年3月批)
墨天轮小教习
124次阅读
2025-04-03 11:43:25
从Percona 发布Pro级产品得到的一些启发
库海无涯
120次阅读
2025-03-26 08:45:23
热门文章
移除DataGuard Standby配置导致Primary启动失败
2023-08-17 21290浏览
使用dblink产生的”SELECT /*+ FULL(P) +*/ * FROM XXXXX P ” 解析
2023-06-20 20891浏览
Troubleshooting 'ORA-28041: Authentication protocol internal error' change password 12c R2 DB
2020-04-08 13638浏览
浅谈ORACLE免费数据库Oracle Database XE (Express Edition) 版
2018-10-31 7585浏览
High wait event ‘row cache mutex’ in 12cR2、19c
2020-08-14 5565浏览