MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

The sql_mode of the MySQL server

Unlike other databases, the MySQL server can run in different SQL modes, and these modes can be applied in different ways for different clients, depending on the value of the sql_mode system variable.
 
The SQL schema defines the SQL syntax and data validation (data validation checks) supported by the MySQL database, making it easier to use MySQL in different environments.
 
In MySQL, SQL schemas are commonly used to solve the following types of problems:
  • By setting the SQL Mode, data verification with different degrees of strictness can be completed, effectively ensuring the accuracy of the data.
  • By setting the SQL Mode to ANSI mode, most of the SQL can be guaranteed to conform to the standard SQL syntax, so that no major modification is required when migrating between different databases.
  • Before migrating data between different databases, setting SQL Mode can make it easier to migrate data in MySQL to the target database.

Common values ​​for the sql_mode system variable

The values ​​that are commonly used in several SQL modes are listed below.

1) TRIICT_ALL_TABLES and STRICT_TRANS_TABLES

If the value of sql_mode is set to TRICT_ALL_TABLES and STRICT_TRANS_TABLES, then MySQL will enable "strict" mode. In strict mode, the MySQL server treats received data more strictly, it will not convert these non-conforming data to the nearest valid value, but will refuse to accept them.

2) TRADITIONAL

Similar to strict mode, but gives an error instead of a warning for inserted unqualified values. It can be applied to transactional tables and non-transactional tables. When used for transactional tables, it will be rolled back as soon as an error occurs.

If you are using a non-transactional storage engine, it is recommended not to set the SQL Mode value to TRADITIONAL, because the operation performed before the error will not be rolled back, which will result in only part of the operation.

3) ANSI_QUOTES

The MySQL server recognizes double quotes as an identifier quote character, not a string quote character. So when ANSI_QUOTES is enabled, double quotes cannot be used to quote strings.

4) PIPES_AS_CONCAT

Causes the MySQL server to treat it ||as a standard SQL string concatenation operator, instead of treating it as a synonym for the OR operator.

In Oracle and other databases, it ||is regarded as a string connection operator, so ||SQL containing operators in other databases will not be executed in MySQL. In order to solve this problem, MySQL provides this value.

5) ANSI

ANSI_QUOTES, PIPES_AS_CONCAT, and several other mode values ​​are enabled at the same time, making the MySQL server behave more like standard SQL than it does by default.

How to set sql_mode

When setting the SQL mode, specify a value consisting of a single mode value or multiple mode values ​​(multiple mode values ​​separated by commas), or specify an empty string to clear the value. Mode values ​​are not case sensitive.
 
If you want to set the SQL mode when you start the server, you can set the system variable sql_mode on the mysqld command line, or in an options file. The following statement can be used:
sql_mode= "TRADITIONAL "
sql_mode= "ANSI_ QUOTES, PIPES_ AS_ CONCAT"

If you just want to change the SQL mode at runtime, you can use the SET statement to set the sql_mode system variable.
SET sql_mode = ' TRADITIONAL' ;
 
If you want to set a global SQL mode, you need to add the GLOBAL keyword:
SET GLOBAL sql_mode = ' TRADITIONAL';
Setting global variables requires SUPER administrative privileges. The newly set global variable value will become the default SQL mode for subsequent connections to the client.
 
If you want to get the current session or global SQL mode value, you can use the following statement:
SELECT @@SESSION.sql_mode;
SELECT @@GLOBAL. sql_mode;
Its return value consists of all currently enabled modes, separated by a comma. Returns a null value if no mode is currently enabled.
  1. How to Check sql_mode in MySQL:

    • Description: Use the following SQL query to check the current sql_mode setting:
      SELECT @@sql_mode;
      
  2. Changing sql_mode in MySQL:

    • Description: To change the sql_mode, you can use the following query:
      SET GLOBAL sql_mode = 'new_sql_mode';
      
      Replace 'new_sql_mode' with the desired SQL mode.
  3. Setting Strict Mode in MySQL:

    • Description: To set strict mode, include STRICT_ALL_TABLES in the sql_mode:
      SET GLOBAL sql_mode = 'STRICT_ALL_TABLES';
      
  4. MySQL sql_mode for Compatibility:

    • Description: For compatibility with other databases, set sql_mode to a value like:
      SET GLOBAL sql_mode = 'MYSQL40';