码迷,mamicode.com
首页 > 数据库 > 详细

Mysql的SQL模式

时间:2015-06-24 00:53:57      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

Mysql在5.6版本以后默认的SQL模式是STRICT_TRANS_TABLES,对数据的严谨性和一致性上比之前版本要有所提升。


一、查看当前sql模式

mysql> select @@sql_mode;
+--------------------------------------------+
| @@sql_mode                                 |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)
二、指定模式
5.6以后默认的my.cnf文件里模式配置是sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,所以启动指定这个配置文件时会自动带上这两个参数值
第一个参数值NO_ENGINE_SUBSTITUTION,是指不支持engine的sql将会报错,支不支持engin可以show engines查看 第二个参数值STRICT_TRANS_TABLES,是指当前会话的严格模式,区别于以往的空,即不严格的模式 
还有其他有意思的参数,如no_auto_create_user等,可以参考官网

三、示例
1.在严格的模式下
mysql> select @@sql_mode;
+--------------------------------------------+
| @@sql_mode                                 |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)

mysql> create table tbl_kenyon(id int null,vname varchar(6));
Query OK, 0 rows affected (0.12 sec)

mysql> insert into tbl_kenyon values(1,‘123456‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbl_kenyon values(1,‘1234567‘);
ERROR 1406 (22001): Data too long for column ‘vname‘ at row 1
mysql> insert into tbl_kenyon values(‘‘,‘123456‘);
ERROR 1366 (HY000): Incorrect integer value: ‘‘ for column ‘id‘ at row 1
mysql> insert into tbl_kenyon values(null,‘123456‘);
Query OK, 1 row affected (0.01 sec)

mysql> select * from tbl_kenyon;
+------+--------+
| id   | vname  |
+------+--------+
|    1 | 123456 |
| NULL | 123456 |
+------+--------+
2 rows in set (0.00 sec)
2.在非严格的模式下
mysql> set @@sql_mode=‘‘;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|            |
+------------+
1 row in set (0.00 sec)

mysql> insert into tbl_kenyon values(99,‘123456‘);
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbl_kenyon values(1,‘1234567‘);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> insert into tbl_kenyon values(‘‘,‘123456‘);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+------------------------------------------------------+
| Level   | Code | Message                                              |
+---------+------+------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: ‘‘ for column ‘id‘ at row 1 |
+---------+------+------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from tbl_kenyon;
+------+--------+
| id   | vname  |
+------+--------+
|    1 | 123456 |
| NULL | 123456 |
|   99 | 123456 |
|    1 | 123456 |
|    0 | 123456 |
+------+--------+
5 rows in set (0.00 sec)
四、 应用场景
两种模式各有利弊,通常生产上会设置严格模式,可以保证数据的完整性;
非严格模式并不一定就是差的,在数据一致性要求不严的场景下某些忽略警告操作可能更方便,比如不同版本数据库的非关键数据导入:
mysql> load data infile ‘/data/source_data/trace/tbl_msg.sql‘ into table tbl_msg;
ERROR 1366 (HY000): Incorrect integer value: ‘NULL‘ for column ‘pid‘ at row 1

换个模式导入:
mysql> set @@session.sql_mode=‘‘;
Query OK, 0 rows affected (0.00 sec)

mysql> load data infile ‘/data/source_data/trace/ tbl_msg.sql‘ into table tbl_msg;
Query OK, 23525 rows affected, 65535 warnings (2.80 sec)
Records: 23525 Deleted: 0 Skipped: 0 Warnings: 185707

五、参考:
https://dev.mysql.com/doc/refman/5.6/en/sql-mode.html

Mysql的SQL模式

标签:

原文地址:http://my.oschina.net/Kenyon/blog/469958

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!