描述

该语句用来设置当前连接的数据库的字符集编码。

格式

  1. SET NAMES 'charset_name' [COLLATE 'collation_name']

参数解释

参数

描述

charset_name

指定字符集

collation_name

指定字符集排序规则。如未指定,则使用字符集默认排序规则。

说明:

该语句将三个会话系统变量 character_set_clientcharacter_set_connectioncharacter_set_results 设置为给定的字符集。将 character_set_connection 设置为 charset_name ,将 collation_connection 设置为 charset_name 的默认排序规则。

示例

  • 设置数据库的字符集和排序规则。
  1. OceanBase(admin@test)>SET NAMES 'utf8mb4' COLLATE 'utf8mb4_general_ci';
  2. Query OK, 0 rows affected (0.00 sec)
  3. OceanBase(admin@test)>show variables like 'character_set_c%';
  4. +--------------------------+---------+
  5. | Variable_name | Value |
  6. +--------------------------+---------+
  7. | character_set_client | utf8mb4 |
  8. | character_set_connection | utf8mb4 |
  9. +--------------------------+---------+
  10. 2 rows in set (0.01 sec)
  11. OceanBase(admin@test)>show variables like 'collation_connection';
  12. +----------------------+--------------------+
  13. | Variable_name | Value |
  14. +----------------------+--------------------+
  15. | collation_connection | utf8mb4_general_ci |
  16. +----------------------+--------------------+
  17. 1 row in set (0.05 sec)
  18. OceanBase(admin@test)>SET NAMES 'gbk';
  19. Query OK, 0 rows affected (0.02 sec)
  20. OceanBase(admin@test)>show variables like 'character_set_c%';
  21. +--------------------------+-------+
  22. | Variable_name | Value |
  23. +--------------------------+-------+
  24. | character_set_client | gbk |
  25. | character_set_connection | gbk |
  26. +--------------------------+-------+
  27. 2 rows in set (0.01 sec)
  28. OceanBase(admin@test)>show variables like 'collation_connection';
  29. +----------------------+----------------+
  30. | Variable_name | Value |
  31. +----------------------+----------------+
  32. | collation_connection | gbk_chinese_ci |
  33. +----------------------+----------------+
  34. 1 row in set (0.01 sec)