-
mysql> show variables like 'sql_mode%'\G;
-
*************************** 1. row ***************************
-
Variable_name: sql_mode
-
Value: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Versions above MySQL 5.0 support three sql_mode modes: ANSI, TRADITIONAL and STICT_TRANS_TABLES.
ANSI mode: Loose mode, check the inserted data, if it does not conform to the defined type or length, adjust or truncate the data type and save, report warning warning.
TRADITIONAL mode: Strict mode, when inserting data into mysql database, the strict verification of data is carried out to ensure that the wrong data can not be inserted and error errors are reported. When used in things, things roll back.
STRICT_TRANS_TABLES mode: Strict mode, strict data verification, error data can not be inserted, error reporting.
1.2 ANSI Model
-
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)
We can see that in ANSI mode, when we insert data, the column length requirement is not met, the data will also be inserted successfully, but truncate the field beyond the column length, and report warning warning warning.
1.3 STRICT_TRANS_TABLES mode