MySQL管理之道-笔记-MySQL5.7 sql_mode的改变

发布时间:2020-06-25 11:57:27 作者:aix6180
来源:网络 阅读:543

MySQL 5.7 sql_mode的改变
1、默认启用STRICT_TRANS_TABLES严格模式,该模式为严格模式,对数据会作严格的校验,错误数据不能插入报错,并且事物回滚。
2、MySQL5.6默认SQL_MODE模式为空。

表age字段是int,插入字符类时会报错,但sql_mode为空,所以数据可以插入。

图1

root@localhost:mysql3306.sock [sbtest]>desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(2) | YES | | NULL | |
| age | smallint(6) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

图2 (sql_mode设置为空)

root@localhost:mysql3306.sock [sbtest]>set sql_mode='';
Query OK, 0 rows affected, 1 warning (0.02 sec)

root@localhost:mysql3306.sock [sbtest]>insert into t1 values(1,'aa','aaa');
Query OK, 1 row affected, 1 warning (0.04 sec)

root@localhost:mysql3306.sock [sbtest]>show warnings;
+---------+------+----------------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: 'aaa' for column 'age' at row 1 |
+---------+------+----------------------------------------------------------+
row in set (0.00 sec)

图3 (插入成功)

root@localhost:mysql3306.sock [sbtest]>select * from t1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | aa | 0 |
+----+------+------+
row in set (0.00 sec)

图4(改成STRICT_TRANS_TABLES,插入失败,事务回滚)

root@localhost:mysql3306.sock [sbtest]>set sql_mode='STRICT_TRANS_TABLES';
Query OK, 0 rows affected, 1 warning (0.00 sec)

root@localhost:mysql3306.sock [sbtest]>insert into t1 values(2,'bb','bbb');
ERROR 1366 (HY000): Incorrect integer value: 'bbb' for column 'age' at row 1
root@localhost:mysql3306.sock [sbtest]>select * from t1 where id=2;
Empty set (0.04 sec)
推荐阅读:
  1. mysql5.7运行时出现3065错误怎么解决
  2. MySQL5.7安装及配置MGR教程

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql 管理之道 -mysql5.7

上一篇:怎么解决运行java代码时出现找不到符号的错误

下一篇:javascript传参到java后台出现乱码的解决方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》