1. SQL_MODE parameter value
The official manual has a dedicated section on https://dev.mysql.com/doc/refman/5.6/en/sql-mode.html . SQL Mode defines two aspects: the SQL syntax that MySQL should support, and what validation checks should be performed on the data.
1. SQL syntax support class
-
ONLY_FULL_GROUP_BY
For GROUP BY aggregation operations, if the columns in the SELECT, HAVING or ORDER BY clauses do not appear in the GROUP BY, then this SQL is invalid. It is understandable, because there will be contradictions if it is not found in the column of group by.
It is enabled by default in 5.7, so you need to pay attention to the process of upgrading from 5.6 to 5.7:
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column '1066export.ebay_order_items.TransactionID' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
-
ANSI_QUOTES
When ANSI_QUOTES is enabled, double quotes cannot be used to quote strings because it is interpreted as an identifier, which acts like `. After setting it,update t set f1="" ...
the syntax error Unknown column '' in 'field list will be reported. -
PIPES_AS_CONCAT
Will be||
treated as a string concatenation operator instead of an OR operator, which is the same as the Oracle database, and is also similar to the string concatenation function CONCAT(). -
NO_TABLE_OPTIONS
When usedSHOW CREATE TABLE
, it will not output MySQL-specific syntax parts. For exampleENGINE
, this needs to be considered when using mysqldump to migrate across DB types. -
NO_AUTO_CREATE_USER
Literally does not automatically create users. When authorizing MySQL users, we are usedGRANT ... ON ... TO dbuser
to creating users along the way. After setting this option, it is similar to oracle operation, users must be established before authorization. 5.7.7 also defaults to the beginning.
2. Data check class
-
NO_ZERO_DATE
The date '0000-00-00' is considered illegal, depending on whether the following strict mode is set.-
NO_ZERO_DATE is naturally satisfied if strict mode is set. But if it is INSERT IGNORE or UPDATE IGNORE, '0000-00-00' is still allowed and only warning is displayed
-
If it is set in non-strict mode
NO_ZERO_DATE
, the effect is the same as the above, '0000-00-00' is allowed but warning is displayed; if not setNO_ZERO_DATE
, no warning is regarded as a completely legal value. -
NO_ZERO_IN_DATE
The situation is similar to the above, the difference is to control the date and day, whether it can be 0, that is,2010-01-00
whether it is legal or not.
-
-
NO_ENGINE_SUBSTITUTION
What should I do if the required storage engine is disabled or not compiled when usingALTER TABLE
or specifying ENGINE.CREATE TABLE
When enabledNO_ENGINE_SUBSTITUTION
, an error is thrown directly; when this value is not set, CREATE is replaced with the default storage engine, ATLER does not make changes, and throws a warning . -
STRICT_TRANS_TABLES
Set it to enable strict mode.
The MySQL that comes with many integrated environments does not seem to have the strict mode of MySQL enabled. What is the strict mode of MySQL? Simply put, MySQL performs strict verification on the data (format, length, type, etc.), such as an integer field. When we write a string type of data, MySQL will not report an error in non-strict mode. Similarly, if a field of type char or varchar is defined, it will not report an error when the data written or updated exceeds the defined length.
I don't think this is any good for programming, although we try to do data validation in the code. MySQL turns on strict mode, which is a test of our code in a certain program. If our development environment does not turn on strict mode and no errors are encountered during the development process, then there may be problems when going online or code porting. Incompatible situation, so it is best to turn on MySQL's strict mode during the development process.
Note that STRICT_TRANS_TABLES
it is not a combination of several strategies. How to deal with a single reference INSERT
, UPDATE
a small value or an invalid value:
-
-
If '' is passed to int, it is illegal in strict mode. If non-strict mode is enabled, it becomes 0, and a warning is generated.
-
Out Of Range, it becomes insert the maximum boundary value
-
A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition
-
The above does not include all SQL Modes, but a few representative ones are selected. For details, please refer to the manual .
sql_mode generally seldom pays attention to it, and will not start or stop the above entries before encountering actual problems. The sql_mode we often set is ANSI
, STRICT_TRANS_TABLES
, TRADITIONAL
, ansi and traditional are several combinations of the above.
-
ANSI
: Change syntax and behavior to be more standard SQL
equivalent to REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE -
TRADITIONAL
: More like traditional SQL database systems, the simple description of this mode is to "give an error instead of a warning" when an incorrect value is inserted in a column.
Equivalent to STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION -
ORACLE
:相当于 PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER
Regardless of the mode, after an error is generated, it means that a single sql fails to execute. For a table that supports transactions, the current transaction will be rolled back; but if it is not executed in a transaction, or a storage engine table that does not support transactions, it may cause Data is inconsistent. MySQL believes that the data inconsistency problem is more serious than direct error termination. So STRICT_TRANS_TABLES
for non-transactional tables, let the writing continue as much as possible, such as giving a "most reasonable" default value or truncation. As for STRICT_ALL_TABLES
, if it is a single update, it will not affect, but if there are multiple updates, the first one succeeds, and the later fails, there will be partial updates.
The default is 5.6.6 and later versions NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
, and the default is '' in 5.5.
2. Set sql_mode
# 1. View the sql mode of the current connection session: mysql> select @@session.sql_mode; or from an environment variable mysql> show variables like "sql_mode"; # 2. View the global sql_mode settings: mysql> select @@global.sql_mode; Only set global, need to reconnect to take effect
set up
#form as mysql> set sql_mode=''; mysql> set global sql_mode='NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES'; #If it is a custom pattern combination, it can be like the following Adding only one mode to sql_mode without removing existing ones: mysql> SET sql_mode=(SELECT CONCAT(@@sql_mode,',<mode_to_add>')); Removing only a specific mode from sql_mode without removing others: mysql> SET sql_mode=(SELECT REPLACE(@@sql_mode,'<mode_to_remove>','')); #Set sql -mode= "" in the configuration file