标签:
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)二、指定模式
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)四、 应用场景
标签:
原文地址:http://my.oschina.net/Kenyon/blog/469958