SET [GLOBAL|SESSION] <variable>

The statement SET [GLOBAL|SESSION] modifies one of TiDB’s built in variables. These variables can be system variables of either SESSION or GLOBAL scope or user variables.

SET <variable> - 图1

Warning

User-defined variables are still an experimental feature. It is NOT recommended that you use them in the production environment.

SET <variable> - 图2

Note

Similar to MySQL, changes to GLOBAL variables do not apply to either existing connections, or the local connection. Only new sessions reflect the changes to the value.

Synopsis

SetVariableStmt

SET <variable> - 图3

Variable

SET <variable> - 图4

  1. SetVariableStmt ::=
  2. "SET" Variable "=" Expression ("," Variable "=" Expression )*
  3. Variable ::=
  4. ("GLOBAL" | "SESSION") SystemVariable
  5. | UserVariable

Examples

Get the value of sql_mode.

  1. mysql> SHOW GLOBAL VARIABLES LIKE 'sql_mode';
  2. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  3. | Variable_name | Value |
  4. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  5. | sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
  6. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> SHOW SESSION VARIABLES LIKE 'sql_mode';
  9. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  10. | Variable_name | Value |
  11. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  12. | sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
  13. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  14. 1 row in set (0.00 sec)

Update the value of sql_mode globally. If you check the value of SQL_mode after the update, you can see that the value of SESSION level has not been updated:

  1. mysql> SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER';
  2. Query OK, 0 rows affected (0.03 sec)
  3. mysql> SHOW GLOBAL VARIABLES LIKE 'sql_mode';
  4. +---------------+-----------------------------------------+
  5. | Variable_name | Value |
  6. +---------------+-----------------------------------------+
  7. | sql_mode | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER |
  8. +---------------+-----------------------------------------+
  9. 1 row in set (0.00 sec)
  10. mysql> SHOW SESSION VARIABLES LIKE 'sql_mode';
  11. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  12. | Variable_name | Value |
  13. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  14. | sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
  15. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  16. 1 row in set (0.00 sec)

Using SET SESSION takes effect immediately:

  1. mysql> SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER';
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> SHOW SESSION VARIABLES LIKE 'sql_mode';
  4. +---------------+-----------------------------------------+
  5. | Variable_name | Value |
  6. +---------------+-----------------------------------------+
  7. | sql_mode | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER |
  8. +---------------+-----------------------------------------+
  9. 1 row in set (0.00 sec)

User variables start with a @.

  1. SET @myvar := 5;
  2. Query OK, 0 rows affected (0.00 sec)
  3. SELECT @myvar, @myvar + 1;
  4. +--------+------------+
  5. | @myvar | @myvar + 1 |
  6. +--------+------------+
  7. | 5 | 6 |
  8. +--------+------------+
  9. 1 row in set (0.00 sec)

MySQL compatibility

The following behavior differences apply:

  • Changes made with SET GLOBAL will be propagated to all TiDB instances in the cluster. This differs from MySQL, where changes do not propagate to replicas.
  • TiDB presents several variables as both readable and settable. This is required for MySQL compatibility, because it is common for both applications and connectors to read MySQL variables. For example: JDBC connectors both read and set query cache settings, despite not relying on the behavior.
  • Changes made with SET GLOBAL will persist through TiDB server restarts. This means that SET GLOBAL in TiDB behaves more similar to SET PERSIST as available in MySQL 8.0 and above.
  • TiDB does not support SET PERSIST and SET PERSIST_ONLY, because TiDB persists global variables.

See also