赞
踩
- mysql> select @@sql_mode;
- +----------------------------------------------------------------+
- | @@sql_mode |
- +----------------------------------------------------------------+
- | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
- +----------------------------------------------------------------+
- mysql> show variables like 'sql_mode%'\G;
- *************************** 1. row ***************************
- Variable_name: sql_mode
- Value: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
- mysql> set @@sql_mode=ANSI;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> create table test(name varchar(4), pass varchar(4));
- Query OK, 0 rows affected (0.03 sec)
-
- mysql> insert into test values('aaaaa','aaaaa'),('bbbb','bbbb');
- Query OK, 2 rows affected, 2 warnings (0.02 sec)
- Records: 2 Duplicates: 0 Warnings: 2
-
- mysql> show warnings;
- +---------+------+-------------------------------------------+
- | Level | Code | Message |
- +---------+------+-------------------------------------------+
- | Warning | 1265 | Data truncated for column 'name' at row 1 |
- | Warning | 1265 | Data truncated for column 'pass' at row 1 |
- +---------+------+-------------------------------------------+
- 2 rows in set (0.00 sec)
-
- mysql> select * from test;
- +------+------+
- | name | pass |
- +------+------+
- | aaaa | aaaa |
- | bbbb | bbbb |
- +------+------+
- 2 rows in set (0.00 sec)
- mysql> set @@sql_mode=STRICT_TRANS_TABLES;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> create table test(name varchar(4), pass varchar(4));
- Query OK, 0 rows affected (0.02 sec)
-
- mysql> insert into test values('aaaaa','aaaaa'),('bbbb','bbbb');
- ERROR 1406 (22001): Data too long for column 'name' at row 1
-
- mysql> show errors;
- +-------+------+------------------------------------------+
- | Level | Code | Message |
- +-------+------+------------------------------------------+
- | Error | 1406 | Data too long for column 'name' at row 1 |
- +-------+------+------------------------------------------+
- 1 row in set (0.00 sec)
-
- mysql> select * from test;
- Empty set (0.00 sec)
- mysql> set @@sql_mode=TRADITIONAL;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> create table test(name varchar(4), pass varchar(4));
- Query OK, 0 rows affected (0.02 sec)
-
- mysql> insert into test values('aaaaa','aaaaa'),('bbbb','bbbb');
- ERROR 1406 (22001): Data too long for column 'name' at row 1
-
- mysql> show errors;
- +-------+------+------------------------------------------+
- | Level | Code | Message |
- +-------+------+------------------------------------------+
- | Error | 1406 | Data too long for column 'name' at row 1 |
- +-------+------+------------------------------------------+
- 1 row in set (0.00 sec)
-
- mysql> select * from test;
- Empty set (0.00 sec)
- mysql> select @@sql_mode\G;
- *************************** 1. row ***************************
- @@sql_mode: STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,E
- RROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER
- 1 row in set (0.00 sec)
注意:我们这里设置的sql_mode都是session级别的。另外,可以直接修改my.ini文件,找到sql_mode,然后设置新的模式即可!
例如:
vi /etc/my.cnf
在[mysqld]下面添加如下列:
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。