• SET [GLOBAL|SESSION]
    • 语法图
    • 示例
    • MySQL 兼容性
    • 另请参阅

    SET [GLOBAL|SESSION]

    SET [GLOBAL|SESSION] 语句用于在 SESSIONGLOBAL 的范围内,对某个 TiDB 的内置变量进行更改。需注意,对 GLOBAL 变量的更改不适用于已有连接或本地连接,这与 MySQL 类似。只有新会话才会反映值的变化。

    语法图

    SetStmt:

    SetStmt

    示例

    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)
    15. mysql> SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER';
    16. Query OK, 0 rows affected (0.03 sec)
    17. mysql> SHOW GLOBAL VARIABLES LIKE 'sql_mode';
    18. +---------------+-----------------------------------------+
    19. | Variable_name | Value |
    20. +---------------+-----------------------------------------+
    21. | sql_mode | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER |
    22. +---------------+-----------------------------------------+
    23. 1 row in set (0.00 sec)
    24. mysql> SHOW SESSION VARIABLES LIKE 'sql_mode';
    25. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
    26. | Variable_name | Value |
    27. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
    28. | 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 |
    29. +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
    30. 1 row in set (0.00 sec)
    31. mysql> SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER';
    32. Query OK, 0 rows affected (0.01 sec)
    33. mysql> SHOW SESSION VARIABLES LIKE 'sql_mode';
    34. +---------------+-----------------------------------------+
    35. | Variable_name | Value |
    36. +---------------+-----------------------------------------+
    37. | sql_mode | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER |
    38. +---------------+-----------------------------------------+
    39. 1 row in set (0.00 sec)

    MySQL 兼容性

    SET [GLOBAL|SESSION] 语句与 MySQL 完全兼容。如有任何兼容性差异,请在 GitHub 上提交 issue。

    另请参阅

    • SHOW [GLOBAL|SESSION] VARIABLES