The MySQL server can operate in different SQL modes, and can apply these modes differently for different clients, depending on the value of the sql_mode
system variable. DBAs can set the global SQL mode to match site server operating requirements, and each application can set its session SQL mode to its own requirements.html
Modes affect the SQL syntax MySQL supports and the data validation checks it performs. This makes it easier to use MySQL in different environments and to use MySQL together with other database servers.mysql
For answers to questions often asked about server SQL modes in MySQL, see Section A.3, 「MySQL 5.7 FAQ: Server SQL Mode」.post
When working with InnoDB
tables, consider also the innodb_strict_mode
system variable. It enables additional error checks forInnoDB
tables.
The default SQL mode in MySQL 5.7 includes these modes: ONLY_FULL_GROUP_BY
, STRICT_TRANS_TABLES
, NO_ZERO_IN_DATE
,NO_ZERO_DATE
, ERROR_FOR_DIVISION_BY_ZERO
, NO_AUTO_CREATE_USER
, and NO_ENGINE_SUBSTITUTION
.
These modes were added to the default SQL mode in MySQL 5.7: The ONLY_FULL_GROUP_BY
and STRICT_TRANS_TABLES
modes were added in MySQL 5.7.5. The NO_AUTO_CREATE_USER
mode was added in MySQL 5.7.7. The ERROR_FOR_DIVISION_BY_ZERO
,NO_ZERO_DATE
, and NO_ZERO_IN_DATE
modes were added in MySQL 5.7.8. For additional discussion regarding these changes to the default SQL mode value, see SQL Mode Changes in MySQL 5.7.
To set the SQL mode at server startup, use the --sql-mode="
option on the command line, or modes
"sql-mode="
in an option file such as modes
"my.cnf
(Unix operating systems) or my.ini
(Windows). modes
is a list of different modes separated by commas. To clear the SQL mode explicitly, set it to an empty string using --sql-mode=""
on the command line, or sql-mode=""
in an option file.
MySQL installation programs may configure the SQL mode during the installation process. If the SQL mode differs from the default or from what you expect, check for a setting in an option file that the server reads at startup.
To change the SQL mode at runtime, set the global or session sql_mode
system variable using a SET
statement:
SET GLOBAL sql_mode = 'modes'; SET SESSION sql_mode = 'modes';
Setting the GLOBAL
variable requires the SUPER
privilege and affects the operation of all clients that connect from that time on. Setting the SESSION
variable affects only the current client. Each client can change its session sql_mode
value at any time.
To determine the current global or session sql_mode
setting, select its value:
SELECT @@GLOBAL.sql_mode; SELECT @@SESSION.sql_mode;
SQL mode and user-defined partitioning. Changing the server SQL mode after creating and inserting data into partitioned tables can cause major changes in the behavior of such tables, and could lead to loss or corruption of data. It is strongly recommended that you never change the SQL mode once you have created tables employing user-defined partitioning.
When replicating partitioned tables, differing SQL modes on the master and slave can also lead to problems. For best results, you should always use the same server SQL mode on the master and slave.
For more information, see Section 22.6, 「Restrictions and Limitations on Partitioning」.
The most important sql_mode
values are probably these:
This mode changes syntax and behavior to conform more closely to standard SQL. It is one of the special combination modeslisted at the end of this section.
If a value could not be inserted as given into a transactional table, abort the statement. For a nontransactional table, abort the statement if the value occurs in a single-row statement or the first row of a multiple-row statement. More details are given later in this section.
As of MySQL 5.7.5, the default SQL mode includes STRICT_TRANS_TABLES
.
Make MySQL behave like a 「traditional」 SQL database system. A simple description of this mode is 「give an error instead of a warning」 when inserting an incorrect value into a column. It is one of the special combination modes listed at the end of this section.
With TRADITIONAL
mode enabled, an INSERT
or UPDATE
aborts as soon as an error occurs. If you are using a nontransactional storage engine, this may not be what you want because data changes made prior to the error may not be rolled back, resulting in a 「partially done」 update.
When this manual refers to 「strict mode,」 it means a mode with either or both STRICT_TRANS_TABLES
or STRICT_ALL_TABLES
enabled.
The following list describes all supported SQL modes:
Do not perform full checking of dates. Check only that the month is in the range from 1 to 12 and the day is in the range from 1 to 31. This may be useful for Web applications that obtain year, month, and day in three different fields and store exactly what the user inserted, without date validation. This mode applies to DATE
and DATETIME
columns. It does not apply TIMESTAMP
columns, which always require a valid date.
With ALLOW_INVALID_DATES
disabled, the server requires that month and day values be legal, and not merely in the range 1 to 12 and 1 to 31, respectively. With strict mode disabled, invalid dates such as '2004-04-31'
are converted to '0000-00-00'
and a warning is generated. With strict mode enabled, invalid dates generate an error. To permit such dates, enableALLOW_INVALID_DATES
.
Treat "
as an identifier quote character (like the `
quote character) and not as a string quote character. You can still use `
to quote identifiers with this mode enabled. With ANSI_QUOTES
enabled, you cannot use double quotation marks to quote literal strings because they are interpreted as identifiers.
The ERROR_FOR_DIVISION_BY_ZERO
mode affects handling of division by zero, which includes MOD(
. For data-change operations (N
,0)INSERT
, UPDATE
), its effect also depends on whether strict SQL mode is enabled.
If this mode is not enabled, division by zero inserts NULL
and produces no warning.
If this mode is enabled, division by zero inserts NULL
and produces a warning.
If this mode and strict mode are enabled, division by zero produces an error, unless IGNORE
is given as well. For INSERT IGNORE
and UPDATE IGNORE
, division by zero inserts NULL
and produces a warning.
For SELECT
, division by zero returns NULL
. Enabling ERROR_FOR_DIVISION_BY_ZERO
causes a warning to be produced as well, regardless of whether strict mode is enabled.
ERROR_FOR_DIVISION_BY_ZERO
is deprecated. ERROR_FOR_DIVISION_BY_ZERO
is not part of strict mode, but should be used in conjunction with strict mode and is enabled by default. A warning occurs if ERROR_FOR_DIVISION_BY_ZERO
is enabled without also enabling strict mode or vice versa. For additional discussion, see SQL Mode Changes in MySQL 5.7.
Because ERROR_FOR_DIVISION_BY_ZERO
is deprecated, it will be removed in a future MySQL release as a separate mode name and its effect included in the effects of strict SQL mode.
The precedence of the NOT
operator is such that expressions such as NOT a BETWEEN b AND c
are parsed as NOT (a BETWEEN b AND c)
. In some older versions of MySQL, the expression was parsed as (NOT a) BETWEEN b AND c
. The old higher-precedence behavior can be obtained by enabling the HIGH_NOT_PRECEDENCE
SQL mode.
mysql> SET sql_mode = ''; mysql> SELECT NOT 1 BETWEEN -5 AND 5; -> 0 mysql> SET sql_mode = 'HIGH_NOT_PRECEDENCE'; mysql> SELECT NOT 1 BETWEEN -5 AND 5; -> 1
Permit spaces between a function name and the (
character. This causes built-in function names to be treated as reserved words. As a result, identifiers that are the same as function names must be quoted as described in Section 9.2, 「Schema Object Names」. For example, because there is a COUNT()
function, the use of count
as a table name in the following statement causes an error:
mysql> CREATE TABLE count (i INT); ERROR 1064 (42000): You have an error in your SQL syntax
The table name should be quoted:
mysql> CREATE TABLE `count` (i INT); Query OK, 0 rows affected (0.00 sec)
The IGNORE_SPACE
SQL mode applies to built-in functions, not to user-defined functions or stored functions. It is always permissible to have spaces after a UDF or stored function name, regardless of whether IGNORE_SPACE
is enabled.
For further discussion of IGNORE_SPACE
, see Section 9.2.4, 「Function Name Parsing and Resolution」.
Prevent the GRANT
statement from automatically creating new user accounts if it would otherwise do so, unless authentication information is specified. The statement must specify a nonempty password using IDENTIFIED BY
or an authentication plugin using IDENTIFIED WITH
.
It is preferable to create MySQL accounts with CREATE USER
rather than GRANT
. NO_AUTO_CREATE_USER
is deprecated and the default SQL mode includes NO_AUTO_CREATE_USER
. Assignments to sql_mode
that change the NO_AUTO_CREATE_USER
mode state produce a warning, except assignments that set sql_mode
to DEFAULT
. NO_AUTO_CREATE_USER
will be removed in a future MySQL release, at which point its effect will be enabled at all times (GRANT
will not create accounts).
Previously, before NO_AUTO_CREATE_USER
was deprecated, one reason not to enable it was that it was not replication safe. Now it can be enabled and replication-safe user management performed with CREATE USER IF NOT EXISTS
, DROP USER IF EXISTS
, and ALTER USER IF EXISTS
rather than GRANT
. These statements enable safe replication when slaves may have different grants than those on the master. See Section 13.7.1.2, 「CREATE USER Syntax」, Section 13.7.1.3, 「DROP USER Syntax」, andSection 13.7.1.1, 「ALTER USER Syntax」.
NO_AUTO_VALUE_ON_ZERO
affects handling of AUTO_INCREMENT
columns. Normally, you generate the next sequence number for the column by inserting either NULL
or 0
into it. NO_AUTO_VALUE_ON_ZERO
suppresses this behavior for 0
so that only NULL
generates the next sequence number.
This mode can be useful if 0
has been stored in a table's AUTO_INCREMENT
column. (Storing 0
is not a recommended practice, by the way.) For example, if you dump the table with mysqldump and then reload it, MySQL normally generates new sequence numbers when it encounters the 0
values, resulting in a table with contents different from the one that was dumped. EnablingNO_AUTO_VALUE_ON_ZERO
before reloading the dump file solves this problem. For this reason, mysqldump automatically includes in its output a statement that enables NO_AUTO_VALUE_ON_ZERO
.
Disable the use of the backslash character (\
) as an escape character within strings and identifiers. With this mode enabled, backslash becomes an ordinary character like any other.
When creating a table, ignore all INDEX DIRECTORY
and DATA DIRECTORY
directives. This option is useful on slave replication servers.
Control automatic substitution of the default storage engine when a statement such as CREATE TABLE
or ALTER TABLE
specifies a storage engine that is disabled or not compiled in.
By default, NO_ENGINE_SUBSTITUTION
is enabled.
Because storage engines can be pluggable at runtime, unavailable engines are treated the same way:
With NO_ENGINE_SUBSTITUTION
disabled, for CREATE TABLE
the default engine is used and a warning occurs if the desired engine is unavailable. For ALTER TABLE
, a warning occurs and the table is not altered.
With NO_ENGINE_SUBSTITUTION
enabled, an error occurs and the table is not created or altered if the desired engine is unavailable.
Do not print MySQL-specific column options in the output of SHOW CREATE TABLE
. This mode is used by mysqldump in portability mode.
As of MySQL 5.7.22, NO_FIELD_OPTIONS
is deprecated. It will be removed in a future version of MySQL.
Do not print MySQL-specific index options in the output of SHOW CREATE TABLE
. This mode is used by mysqldump in portability mode.
As of MySQL 5.7.22, NO_KEY_OPTIONS
is deprecated. It will be removed in a future version of MySQL.
Do not print MySQL-specific table options (such as ENGINE
) in the output of SHOW CREATE TABLE
. This mode is used by mysqldump in portability mode.
As of MySQL 5.7.22, NO_TABLE_OPTIONS
is deprecated. It will be removed in a future version of MySQL.
Subtraction between integer values, where one is of type UNSIGNED
, produces an unsigned result by default. If the result would otherwise have been negative, an error results:
mysql> SET sql_mode = ''; Query OK, 0 rows affected (0.00 sec) mysql> SELECT CAST(0 AS UNSIGNED) - 1; ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(cast(0 as unsigned) - 1)'
If the NO_UNSIGNED_SUBTRACTION
SQL mode is enabled, the result is negative:
mysql> SET sql_mode = 'NO_UNSIGNED_SUBTRACTION'; mysql> SELECT CAST(0 AS UNSIGNED) - 1; +-------------------------+ | CAST(0 AS UNSIGNED) - 1 | +-------------------------+ | -1 | +-------------------------+
If the result of such an operation is used to update an UNSIGNED
integer column, the result is clipped to the maximum value for the column type, or clipped to 0 if NO_UNSIGNED_SUBTRACTION
is enabled. With strict SQL mode enabled, an error occurs and the column remains unchanged.
When NO_UNSIGNED_SUBTRACTION
is enabled, the subtraction result is signed, even if any operand is unsigned. For example, compare the type of column c2
in table t1
with that of column c2
in table t2
:
mysql> SET sql_mode=''; mysql> CREATE TABLE test (c1 BIGINT UNSIGNED NOT NULL); mysql> CREATE TABLE t1 SELECT c1 - 1 AS c2 FROM test; mysql> DESCRIBE t1; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | c2 | bigint(21) unsigned | NO | | 0 | | +-------+---------------------+------+-----+---------+-------+ mysql> SET sql_mode='NO_UNSIGNED_SUBTRACTION'; mysql> CREATE TABLE t2 SELECT c1 - 1 AS c2