MySQL learning notes SQL server mode summary MySQL servers can be operated in different SQL modes, and different modes can be applied for different clients. In this way, each application can customize the server operation mode as needed. The mode defines which SQL statements MySQL servers can operate in different SQL modes and can apply different modes to different clients. In this way, each application can customize the server operation mode as needed.
The mode defines which SQL syntaxes should be supported by MySQL and which data verification checks should be performed. This makes it easier to use MySQL in different environments and use it with other database servers.
You can use the-SQL-mode = "modes" option to start mysqld to set the default SQL mode. If you want to reset it, the value can also be blank (-SQL-mode = "").
You can also use the SET [SESSION | GLOBAL] SQL _mode = 'modes 'statement to SET the SQL _mode variable after startup to change the SQL mode. The SUPER permission is required when setting GLOBAL variables, and operations on all clients connected from that time will be affected. Setting SESSION variables only affects the current client. Any client can change its own session SQL _mode value at any time.
Modesis is a series of different modes separated by commas. You can use the SELECT @ SQL _mode statement to query the current mode. The default value is null (no mode is set ).
The main important SQL _mode value is:
·ANSI
Modify the syntax and behavior to make it more compliant with standard SQL.
The preceding operations show that after SQL _mode is set to ANSI, the supported SQL syntax, data verification check, and other attributes are changed accordingly.
·STRICT_TRANS_TABLES
If the given value cannot be inserted into the transaction table, discard the statement. For non-transaction tables, if the value appears in the 1st rows of a single-row or multi-row statement, the statement is discarded. A more detailed description is provided later in this section.
·TRADITIONAL
This SQL _mode mode makes MySQL behave like a "traditional" SQL database system. A simple description of this mode is to "give an error rather than warning" when an incorrect value is inserted in the column ". If an error is found, immediately discard INSERT/UPDATE. If you use a non-transaction storage engine, this method is not what you want, because the data changes made before an error are not "rolled", and the result is that the update is "only a part ".
"STRICT Mode" indicates the mode in which STRICT _ TRANS_TABLES or STRICT _ ALL_TABLES are enabled.
All modes supported by SQL _mode:
· ALLOW_INVALID_DATES
Do not check all dates in strict mode. Only check the day between the month and the day between 1 and 12. This is important in Web applications when you get the year, month, and day from three different fields and want to exactly save the content inserted by the user (without date verification. This mode applies to DATE and DATETIME columns. It is not suitable for the TIMESTAMP column. the TIMESTAMP column requires the verification date.
After the strict mode is enabled, the server requires valid months and days, not only in the range of 1 to 12 and 1 to 31, respectively. For example, '2017-04-31 'is valid when strict mode is disabled, but it is invalid when strict mode is enabled. ALLOW_INVALID_DATES should also be enabled to allow the masking of fixed dates in strict mode.
· ANSI_QUOTES
The double quotation mark ("") is regarded as an identifier quotation mark, not a string quotation mark character. In ANSI mode, you can still use ''' to reference the identifier. When ANSI_QUOTES is enabled, you cannot use double quotation marks to reference strings because it is interpreted as an identifier. For example:
· ERROR_FOR_DIVISION_BY_ZERO
In strict mode, if the INSERT or UPDATE process is divided by zero (or MOD (X, 0), an error is generated (otherwise it is a warning ). If this mode is not provided, MySQL returns NULL if it is divided by zero. For example, if the insert ignore or update ignore mode is used, MySQL generates a zero division warning, but the operation result is NULL.
· HIGH_NOT_PRECEDENCE
The priority of the NOT operator is that the expression, for example, NOT a BETWEEN B AND c, is interpreted as NOT (a BETWEEN B AND c ). In some earlier versions of MySQL, the expression is interpreted as (NOT a) BETWEEN B AND c. Enable the HIGH_NOT_PRECEDENCESQL mode to obtain results with a higher priority.
For example:
· IGNORE_SPACE
A space is allowed between the function name and. All function names are considered as saved words. The result is that if you want to access the database, table, or column name that is saved as a word, you must reference it. For example, because there is a USER () function, the user table name in the mysql database and the User column in the table are saved, so you must reference them:
SELECT "User" FROM mysql."user";
· NO_AUTO_CREATE_USER
Prevent GRANT from automatically creating new users unless a password is specified.
· NO_AUTO_VALUE_ON_ZERO
NO_AUTO_VALUE_ON_ZERO affects the processing of the AUTO_INCREMENT column. Generally, you can insert NULL or 0 to this column to generate the next serial number. NO_AUTO_VALUE_ON_ZERO disables 0, so only NULL can generate the next serial number.
This mode is useful if you save 0 to the AUTO_INCREMENT column of the table. (This convention is not recommended ). For example, if you use mysqldump to dump a table and reload the table, MySQL generates a new serial number when it encounters a value of 0. the contents of the generated table are different from those of the dump table. Enable NO_AUTO_VALUE_ON_ZERO before reloading the dump file to solve this problem. Mysqldump automatically includes the NO_AUTO_VALUE_ON_ZERO statement in the output.
· NO_BACKSLASH_ESCAPES
Disables the backslash ('\') as the exit character in the string. If this mode is enabled, the backslash is a common character.
· NO_DIR_IN_CREATE
Ignore all index directory and data directory commands when creating a table. This option is useful for slave replication servers.
· NO_ENGINE_SUBSTITUTION
If the required storage engine is disabled or not compiled, the storage engine cannot be replaced automatically.
· NO_FIELD_OPTIONS
Do not print the MySQL special column option in the output of show create table. This mode is used for mysqldump in portable mode.
· NO_KEY_OPTIONS
Do not print MySQL-specific index options in the output of show create table. This mode is used for mysqldump in portable mode.
· NO_TABLE_OPTIONS
Do not print the MySQL special TABLE option (such as ENGINE) in the output of show create table ). This mode is used for mysqldump in portable mode.
· NO_UNSIGNED_SUBTRACTION
In a subtraction operation, if an operand has no symbols, do not mark the result as UNSIGNED. Note that the unsigned bigint cannot be 100% used in context.
· NO_ZERO_DATE
In strict mode, do not use '2017-00-00 'as a valid date. You can still use the IGNORE option to insert a zero date. In non-strict mode, this date is acceptable, but a warning is generated.
· NO_ZERO_IN_DATE
In strict mode, the date of month or day is not accepted. If the IGNORE option is used, we insert '2017-00-00 'for a similar date '. In non-strict mode, this date is acceptable, but a warning is generated.
· ONLY_FULL_GROUP_BY
Do not point the query in the group by section to an unselected column.
· PIPES_AS_CONCAT
| Is treated as a string concatenation operator (+) (same as CONCAT (), not as OR.
· REAL_AS_FLOAT
Treat REAL as a synonym for FLOAT, rather than a synonym for DOUBLE.
· STRICT_TRANS_TABLES
Enable strict mode for all storage engines. The invalid data value is denied.
· STRICT_TRANS_TABLES
Enable strict mode for the transaction storage engine, or enable strict mode for the non-transaction storage engine.
Strictly control how MySQL processes illegal or lost input values. One value can be invalid for several reasons. For example, the data type is incorrect. it is not suitable for columns or is out of range. If the newly inserted row does not contain a column that does not display the value defined by the DEFAULT clause, the value is lost.
When the STRICT_ALL_TABLES or STRICT_TRANS_TABLES mode is enabled for a transaction table, an error occurs if the statement contains an invalid or missing value. The statement is abandoned and rolled.
For non-transaction tables, if the 1st rows inserted or updated have bad values, the two modes share the same behavior. The statement is abandoned and the table remains unchanged. If the statement inserts or modifies multiple rows and the bad value appears in the 2nd or later rows, the result depends on which strict option is enabled:
For STRICT_ALL_TABLES, MySQL returns an error and ignores the remaining rows. However, in this case, the previous row has been inserted or updated. This indicates that you can update some data, which may not be what you want. To avoid this, it is best to use a single row statement, because you can give up without changing the table.
For STRICT_TRANS_TABLES, MySQL Converts invalid values to valid values closest to the column and inserts the adjusted values. If the value is lost, MySQL inserts an implicit default value in the column. In any case, MySQL generates a warning instead of an error and continues to execute the statement.
Invalid date is not allowed in strict mode, for example, '2017-04-31 '. It does not allow the date to use the "zero" part, for example, '2017-04-00' or "zero" date. To disable it, enable the NO_ZERO_IN_DATE and NO_ZERO_DATE SQL modes based on the strict mode.
If you do not use the strict mode (that is, do not enable STRICT_TRANS_TABLES or STRICT_ALL_TABLES mode), MySQL inserts the adjusted value and provides a warning for invalid or lost values. In strict mode, you can use insert ignore or update ignore.
The following special modes quickly combine one or more of the preceding modes.
This includes all the mode values in most of the latest MySQL versions. In the old version, the combination mode does not include any unsuitable mode values in the new version.
ANSI
It is equivalent to REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, and IGNORE_SPACE.
DB2
Equivalent to PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, NO_TABLE_OPTIONS, and NO_FIELD_OPTIONS.
MAXDB
Equivalent to PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, and NO_AUTO_CREATE_USER.
MSSQL
Equivalent to PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, NO_TABLE_OPTIONS, and NO_FIELD_OPTIONS.
MYSQL323
It is equivalent to NO_FIELD_OPTIONS and HIGH_NOT_PRECEDENCE.
MYSQL40
It is equivalent to NO_FIELD_OPTIONS and HIGH_NOT_PRECEDENCE.
ORACLE
Equivalent to PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, and NO_AUTO_CREATE_USER.
POSTGRESQL
Equivalent to PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, NO_TABLE_OPTIONS, and NO_FIELD_OPTIONS.
TRADITIONAL
It is equivalent to STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, and NO_AUTO_CREATE_USER.
If you want to assemble different modes by yourself, you only need to separate the different modes with commas during set: