sql_mode of mysql

#Set global variables

mysql> select @@global.sql_mode;
+----------------------------------------------------------------------------------------------------+
| @@global.sql_mode                                                                                  |
+----------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> set @@global.sql_mode=(select replace(@@global.sql_mode,'NO_ZERO_DATE',''));
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select @@global.sql_mode;
+---------------------------------------------------------------------------------------+
| @@global.sql_mode                                                                     |
+---------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+---------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

#Set session variables

mysql> select @@session.sql_mode; 
+----------------------------------------------------------------------------------------------------+
| @@session.sql_mode                                                                                 |
+----------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> set @@session.sql_mode=(select replace(@@session.sql_mode,'NO_ZERO_DATE',''));              
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select @@session.sql_mode;
+---------------------------------------------------------------------------------------+
| @@session.sql_mode                                                                    |
+---------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

 

# The global setting above becomes invalid after restart and needs to be modified in the database configuration file

# Add the following configuration in my.cnf

    [mysqld]

    sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

# The specific sql_mode still needs to check the official website, the following is the commonly used sql_mode 

Common values ​​of sql_mode

  • 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 illegal, because the column is not in the GROUP BY clause

  • NO_AUTO_VALUE_ON_ZERO

This value affects the insertion of self-increasing columns. By default, inserting 0 or NULL means that the next self-increasing value is generated. If the user wants to insert a value of 0, and the column is self-growing, then this option is useful.

  • STRICT_TRANS_TABLES

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

  • NO_ZERO_IN_DATE

In strict mode, zero days and months are not allowed

  • NO_ZERO_DATE

Set this value, mysql database does not allow to insert zero date, inserting zero date will throw an error instead of a warning

  • ERROR_FOR_DIVISION_BY_ZERO

During insert or update, if the 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

Prohibit GRANT from creating users with empty passwords

  • NO_ENGINE_SUBSTITUTION

If the required storage engine is disabled or not compiled, then an error is thrown. When this value is not set, replace with the default storage engine and throw an exception

  • PIPES_AS_CONCAT

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

  • ANSI_QUOTES

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

Guess you like

Origin blog.csdn.net/ganices/article/details/113989240