云数据库OceanBase入门教程 OceanBase SET NAMES

2024-02-26 开发教程 云数据库OceanBase入门教程 匿名 0

描述

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

格式

SET NAMES 'charset_name' [COLLATE 'collation_name']

参数解释

参数

描述

charset_name

指定字符集

collation_name

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

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

示例

  • 设置数据库的字符集和排序规则。
OceanBase(admin@test)>SET NAMES 'utf8mb4' COLLATE 'utf8mb4_general_ci';
Query OK, 0 rows affected (0.00 sec)
OceanBase(admin@test)>show variables like 'character_set_c%';
+--------------------------+---------+
| Variable_name | Value |
+--------------------------+---------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
+--------------------------+---------+
2 rows in set (0.01 sec)
OceanBase(admin@test)>show variables like 'collation_connection';
+----------------------+--------------------+
| Variable_name | Value |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
+----------------------+--------------------+
1 row in set (0.05 sec)
OceanBase(admin@test)>SET NAMES 'gbk';
Query OK, 0 rows affected (0.02 sec)
OceanBase(admin@test)>show variables like 'character_set_c%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| character_set_client | gbk |
| character_set_connection | gbk |
+--------------------------+-------+
2 rows in set (0.01 sec)
OceanBase(admin@test)>show variables like 'collation_connection';
+----------------------+----------------+
| Variable_name | Value |
+----------------------+----------------+
| collation_connection | gbk_chinese_ci |
+----------------------+----------------+
1 row in set (0.01 sec)