MYSQL database schema

In the MySQL database, the SQL mode can be used to solve data verification of different degrees of strictness, and the purpose of migration can be achieved when data migration is performed in different databases. In the production environment, this value must be set to strict mode ( strict mode refers to setting the sql_mode variable to at least one of STRICT_TRANS_TABLES or STRICT_ALL_TABLES ), so the database of the development and test environments must also be set, so that in the development and testing phase, you can problem found.

ONLY_FULL_GROUP_BY:

For GROUP BY aggregation operations, if the column in the SELECT does not appear in the GROUP BY, then this SQL is not valid, because the column is not in the GROUP BY clause


NO_AUTO_VALUE_ON_ZERO:

This value affects the insertion of self-growing columns. By default, inserting 0 or NULL generates the next auto-increment value. This option is useful if the user wants to insert a value of 0 and the column is auto-incrementing.


STRICT_TRANS_TABLES:

In this mode, if a value cannot be inserted into a transactional table, the current operation is interrupted, and there is no restriction on non-transactional tables

NO_ZERO_IN_DATE:

In strict mode, the day and month are not allowed to be zero


NO_ZERO_DATE:

Set this value, the MySQL database does not allow zero dates to be inserted, inserting zero dates will throw an error instead of a warning.


ERROR_FOR_DIVISION_BY_ZERO:

During an INSERT or UPDATE, if data is divided by zero, an error is generated instead of a warning. If this mode is not given, MySQL returns NULL when the data is divided by zero


NO_AUTO_CREATE_USER

Disable GRANT from creating users with empty passwords


NO_ENGINE_SUBSTITUTION

Throws an error if the required storage engine is disabled or not compiled. MYSQL can specify the ENGINE clause in CREATE TABLE. When this value is not set, the default storage engine is used instead, and an exception is thrown


PIPES_AS_CONCAT:

Treat "||" as a string concatenation operator instead of an OR operator, which is the same as the Oracle database and similar to the string concatenation function Concat


ANSI_QUOTES:

When ANSI_QUOTES is enabled, double quotes cannot be used to quote a string as it is interpreted as an identifier

 

ANSI mode

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 |
+------+------+
| yyyy | yyyy |
| bbbb | bbbb |
+------+------+
2 rows in set (0.00 sec)

We can see that in ANSI mode, when we insert data and the column length requirements are not met, the data will also be inserted successfully, but the fields exceeding the column length will be truncated and a warning will be reported.

 

STRICT_TRANS_TABLES mode

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)

We can see that in the STRICT_TRANS_TABLES mode, when we insert data, mysql will strictly verify the data. When it is found that the inserted column value does not meet the requirements, it will report the error directly, ensuring that the wrong data cannot be inserted into the database.

 

TRADITIONAL mode

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)

The results of TRADITIONAL mode and STRICT_TRANS_TABLES mode execution are consistent in this case.

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)

Query global schema settings

SELECT@@global.sql_mode

 Query the SQL mode of the current session

SELECT@@session.sql_mode
SELECT@@sql_mode
clear mode
SET sql_mode=''
Fault tolerance will be done in the case of no mode

Note: The sql_mode we set here is at the session level. In addition, you can directly modify the my.ini file, find sql_mode , and then set a new mode!

Development environment configuration

STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER

If you use mysql, in order to keep the habit of using oracle, you can set the sql_mode of mysql as follows :

[mysqld]
sql_mode='ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT,ANSI_QUOTES'

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326219719&siteId=291194637