Skip to content

tidb: add description about GB18030 #18662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,7 @@
- 字符集和排序规则
- [概述](/character-set-and-collation.md)
- [GBK](/character-set-gbk.md)
- [GB18030](/character-set-gb18030.md)
- [Placement Rules in SQL](/placement-rules-in-sql.md)
- 系统表
- `mysql` Schema
Expand Down
1 change: 1 addition & 0 deletions br/backup-and-restore-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ TiDB 支持将数据备份到 Amazon S3、Google Cloud Storage (GCS)、Azure Blo
| 功能 | 相关 issue | 解决方式 |
| ---- | ---- | ----- |
|GBK charset|| BR 在 v5.4.0 之前不支持恢复 `charset=GBK` 的表。并且,任何版本的 BR 都不支持恢复 `charset=GBK` 的表到 v5.4.0 之前的 TiDB 集群。|
|GB18030 charset|| BR 在 v9.0.0 之前不支持恢复 `charset=GB18030` 的表。并且,任何版本的 BR 都不支持恢复 `charset=GB18030` 的表到 v9.0.0 之前的 TiDB 集群。|
| 聚簇索引 | [#565](https://github.com/pingcap/br/issues/565) | 确保恢复时集群的 `tidb_enable_clustered_index` 全局变量和备份时一致,否则会导致数据不一致的问题,例如 `default not found` 和数据索引不一致。 |
| New collation | [#352](https://github.com/pingcap/br/issues/352) | 确保恢复时集群的 `mysql.tidb` 表中 `new_collation_enabled` 变量值和备份时的一致,否则会导致数据索引不一致和 checksum 通不过。更多信息,请参考 [FAQ - BR 为什么会报 `new_collations_enabled_on_first_bootstrap` 不匹配?](/faq/backup-and-restore-faq.md#恢复时为什么会报-new_collation_enabled-不匹配)。 |
| 全局临时表 | | 确保使用 BR v5.3.0 及以上版本进行备份和恢复,否则会导致全局临时表的表定义错误。 |
Expand Down
41 changes: 23 additions & 18 deletions character-set-and-collation.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: 字符集和排序规则
aliases: ['/docs-cn/dev/character-set-and-collation/','/docs-cn/dev/reference/sql/characterset-and-collation/','/docs-cn/dev/reference/sql/character-set/']
summary: TiDB 支持的字符集包括 ascii、binary、gbk、latin1、utf8 和 utf8mb4。排序规则包括 ascii_bin、binary、gbk_bin、gbk_chinese_ci、latin1_bin、utf8_bin、utf8_general_ci、utf8_unicode_ci、utf8mb4_0900_ai_ci、utf8mb4_0900_bin、utf8mb4_bin、utf8mb4_general_ci 和 utf8mb4_unicode_ci。TiDB 强烈建议使用 utf8mb4 字符集,因为它支持更多字符。在 TiDB 中,默认的排序规则受到客户端的连接排序规则设置的影响。如果客户端使用 utf8mb4_0900_ai_ci 作为连接排序规则,TiDB 将遵循客户端的配置。TiDB 还支持新的排序规则框架,用于在语义上支持不同的排序规则。
summary: TiDB 支持的字符集包括 ascii、binary、gbk、gb18030、latin1、utf8 和 utf8mb4。排序规则包括 ascii_bin、binary、gbk_bin、gbk_chinese_ci、gb18030_bin、gb18030_chinese_ci、latin1_bin、utf8_bin、utf8_general_ci、utf8_unicode_ci、utf8mb4_0900_ai_ci、utf8mb4_0900_bin、utf8mb4_bin、utf8mb4_general_ci 和 utf8mb4_unicode_ci。TiDB 强烈建议使用 utf8mb4 字符集,因为它支持更多字符。在 TiDB 中,默认的排序规则受到客户端的连接排序规则设置的影响。如果客户端使用 utf8mb4_0900_ai_ci 作为连接排序规则,TiDB 将遵循客户端的配置。TiDB 还支持新的排序规则框架,用于在语义上支持不同的排序规则。
---

# 字符集和排序规则
Expand Down Expand Up @@ -98,18 +98,19 @@ SELECT
SHOW CHARACTER SET;
```

```
+---------+-------------------------------------+-------------------+--------+
| Charset | Description | Default collation | Maxlen |
+---------+-------------------------------------+-------------------+--------+
| ascii | US ASCII | ascii_bin | 1 |
| binary | binary | binary | 1 |
| gbk | Chinese Internal Code Specification | gbk_bin | 2 |
| latin1 | Latin1 | latin1_bin | 1 |
| utf8 | UTF-8 Unicode | utf8_bin | 3 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_bin | 4 |
+---------+-------------------------------------+-------------------+--------+
6 rows in set (0.00 sec)
```sql
+---------+-------------------------------------+--------------------+--------+
| Charset | Description | Default collation | Maxlen |
+---------+-------------------------------------+--------------------+--------+
| ascii | US ASCII | ascii_bin | 1 |
| binary | binary | binary | 1 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
| gbk | Chinese Internal Code Specification | gbk_chinese_ci | 2 |
| latin1 | Latin1 | latin1_bin | 1 |
| utf8 | UTF-8 Unicode | utf8_bin | 3 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_bin | 4 |
+---------+-------------------------------------+--------------------+--------+
7 rows in set (0.000 sec)
```

TiDB 支持以下排序规则:
Expand All @@ -118,12 +119,14 @@ TiDB 支持以下排序规则:
SHOW COLLATION;
```

```
```sql
+--------------------+---------+-----+---------+----------+---------+---------------+
| Collation | Charset | Id | Default | Compiled | Sortlen | Pad_attribute |
+--------------------+---------+-----+---------+----------+---------+---------------+
| ascii_bin | ascii | 65 | Yes | Yes | 1 | PAD SPACE |
| binary | binary | 63 | Yes | Yes | 1 | NO PAD |
| gb18030_bin | gb18030 | 249 | | Yes | 1 | PAD SPACE |
| gb18030_chinese_ci | gb18030 | 248 | Yes | Yes | 1 | PAD SPACE |
| gbk_bin | gbk | 87 | | Yes | 1 | PAD SPACE |
| gbk_chinese_ci | gbk | 28 | Yes | Yes | 1 | PAD SPACE |
| latin1_bin | latin1 | 47 | Yes | Yes | 1 | PAD SPACE |
Expand All @@ -136,7 +139,7 @@ SHOW COLLATION;
| utf8mb4_general_ci | utf8mb4 | 45 | | Yes | 1 | PAD SPACE |
| utf8mb4_unicode_ci | utf8mb4 | 224 | | Yes | 8 | PAD SPACE |
+--------------------+---------+-----+---------+----------+---------+---------------+
13 rows in set (0.00 sec)
15 rows in set (0.000 sec)
```

> **警告:**
Expand Down Expand Up @@ -170,7 +173,7 @@ SHOW COLLATION WHERE Charset = 'utf8mb4';
5 rows in set (0.00 sec)
```

TiDB 对 GBK 字符集的支持详情见 [GBK](/character-set-gbk.md)。
TiDB 对 GBK 字符集的支持详情见 [GBK](/character-set-gbk.md),对 GB18030 字符集的支持详情见 [GB18030](/character-set-gb18030.md)

## TiDB 中的 `utf8` 和 `utf8mb4`

Expand Down Expand Up @@ -519,9 +522,11 @@ SELECT VARIABLE_VALUE FROM mysql.tidb WHERE VARIABLE_NAME='new_collation_enabled
1 row in set (0.00 sec)
```

在新的排序规则框架下,TiDB 能够支持 `utf8_general_ci`、`utf8mb4_general_ci`、`utf8_unicode_ci`、`utf8mb4_unicode_ci`、`utf8mb4_0900_bin`、`utf8mb4_0900_ai_ci`、`gbk_chinese_ci` 和 `gbk_bin` 这几种排序规则,与 MySQL 兼容。
在新的排序规则框架下,TiDB 能够支持 `utf8_general_ci`、`utf8mb4_general_ci`、`utf8_unicode_ci`、`utf8mb4_unicode_ci`、`utf8mb4_0900_bin`、`utf8mb4_0900_ai_ci`、`gbk_chinese_ci`、`gbk_bin`、`gb18030_chinese_ci` 和 `gb18030_bin` 这几种排序规则,与 MySQL 兼容。

使用 `utf8_general_ci`、`utf8mb4_general_ci`、`utf8_unicode_ci`、`utf8mb4_unicode_ci`、`utf8mb4_0900_ai_ci`、`gbk_chinese_ci` 和 `gb18030_chinese_ci` 中任一种时,字符串之间的比较是大小写不敏感 (case-insensitive) 和口音不敏感 (accent-insensitive) 的。同时,TiDB 还修正了排序规则的 `PADDING` 行为:

使用 `utf8_general_ci`、`utf8mb4_general_ci`、`utf8_unicode_ci`、`utf8mb4_unicode_ci`、`utf8mb4_0900_ai_ci` 和 `gbk_chinese_ci` 中任一种时,字符串之间的比较是大小写不敏感 (case-insensitive) 和口音不敏感 (accent-insensitive) 的。同时,TiDB 还修正了排序规则的 `PADDING` 行为:
{{< copyable "sql" >}}

```sql
CREATE TABLE t(a varchar(20) charset utf8mb4 collate utf8mb4_general_ci PRIMARY KEY);
Expand Down
111 changes: 111 additions & 0 deletions character-set-gb18030.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
title: GB18030
summary: 本文介绍 TiDB 对 GB18030 字符集的支持情况。
---

# GB18030

TiDB 从 v9.0.0 开始支持 GB18030-2022 字符集。本文档介绍 TiDB 对 GB18030 字符集的支持和兼容情况。

```sql
SHOW CHARACTER SET WHERE CHARSET = 'gb18030';
```

```
+---------+---------------------------------+--------------------+--------+
| Charset | Description | Default collation | Maxlen |
+---------+---------------------------------+--------------------+--------+
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+---------+---------------------------------+--------------------+--------+
1 row in set (0.01 sec)
```

```sql
SHOW COLLATION WHERE CHARSET = 'gb18030';
```

```
+--------------------+---------+-----+---------+----------+---------+---------------+
| Collation | Charset | Id | Default | Compiled | Sortlen | Pad_attribute |
+--------------------+---------+-----+---------+----------+---------+---------------+
| gb18030_bin | gb18030 | 249 | | Yes | 1 | PAD SPACE |
| gb18030_chinese_ci | gb18030 | 248 | Yes | Yes | 1 | PAD SPACE |
+--------------------+---------+-----+---------+----------+---------+---------------+
2 rows in set (0.001 sec)
```

## 与 MySQL 的兼容性

本节介绍 TiDB 中 GB18030 字符集与 MySQL 的兼容情况。

### 排序规则兼容性

MySQL 的 GB18030 字符集默认排序规则是 `gb18030_chinese_ci`。TiDB 的 GB18030 字符集的默认排序规则取决于 TiDB 配置项[`new_collations_enabled_on_first_bootstrap`](/tidb-configuration-file.md#new_collations_enabled_on_first_bootstrap) 的值:

- 默认情况下,TiDB 配置项 [`new_collations_enabled_on_first_bootstrap`](/tidb-configuration-file.md#new_collations_enabled_on_first_bootstrap) 为 `true`,表示开启[新的排序规则框架](/character-set-and-collation.md#新框架下的排序规则支持)。GB18030 字符集的默认排序规则是 `gb18030_chinese_ci`。
- 当 TiDB 配置项 [`new_collations_enabled_on_first_bootstrap`](/tidb-configuration-file.md#new_collations_enabled_on_first_bootstrap) 为 `false` 时,表示关闭新的排序规则框架,GB18030 字符集的默认排序规则是 `gb18030_bin`。

另外,TiDB 支持的 `gb18030_bin` 与 MySQL 支持的 `gb18030_bin` 排序规则也不一致,TiDB 是将 GB18030 转换成 `utf8mb4`,然后再进行二进制排序。

开启新的排序规则框架后,查看 GB18030 字符集对应的排序规则,可以看到 TiDB GB18030 默认排序规则已经切换为 `gb18030_chinese_ci`。

```sql
SHOW CHARACTER SET WHERE CHARSET = 'gb18030';
```

```
+---------+---------------------------------+--------------------+--------+
| Charset | Description | Default collation | Maxlen |
+---------+---------------------------------+--------------------+--------+
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+---------+---------------------------------+--------------------+--------+
1 row in set (0.01 sec)
```

```sql
SHOW COLLATION WHERE CHARSET = 'gb18030';
```

```
+--------------------+---------+-----+---------+----------+---------+---------------+
| Collation | Charset | Id | Default | Compiled | Sortlen | Pad_attribute |
+--------------------+---------+-----+---------+----------+---------+---------------+
| gb18030_bin | gb18030 | 249 | | Yes | 1 | PAD SPACE |
| gb18030_chinese_ci | gb18030 | 248 | Yes | Yes | 1 | PAD SPACE |
+--------------------+---------+-----+---------+----------+---------+---------------+
2 rows in set (0.00 sec)
```

### 字符兼容性

- TiDB 支持 GB18030-2022 的字符,MySQL 支持 GB18030-2005 的字符,因此部分字符的编解码结果不同。

- 对于非法的 GB18030 字符,比如 `0xFE39FE39`,MySQL 支持以 16 进制的方式写入数据库中,并保存为 `?`;TiDB 在严格模式下读写非法 GB18030 字符都会报错,在非严格模式下,读写非法 GB18030 字符会返回警告。

### 其它

* 目前 TiDB 不支持通过 `ALTER TABLE` 语句将其它字符集类型改成 `gb18030` 或者从 `gb18030` 转成其它字符集类型。

* TiDB 不支持使用 `_gb18030`,比如:

```sql
CREATE TABLE t(a CHAR(10) CHARSET BINARY);
Query OK, 0 rows affected (0.00 sec)
INSERT INTO t VALUES (_gb18030'啊');
ERROR 1115 (42000): Unsupported character introducer: 'gb18030'
```

* 对于 `ENUM` 和 `SET` 类型中的二进制字符,TiDB 目前都会将其作为 `utf8mb4` 字符集处理。

## 组件兼容性

* TiFlash、TiDB Data Migration (DM) 和 TiCDC 目前不支持 GB18030 字符集。

* Dumpling 在 v9.0.0 之前不支持导出 charset=GB18030 的表,TiDB Lightning 在 v9.0.0 之前不支持导入 charset=GB18030 的表。

* TiDB Backup & Restore(BR)在 v9.0.0 之前不支持备份恢复 charset=GB18030 的表。另外,任何版本的 BR 都不支持恢复 charset=GB18030 的表到 v9.0.0 之前的平凯数据库集群。

## 另请参阅

* [`SHOW CHARACTER SET`](/sql-statements/sql-statement-show-character-set.md)
* [字符集和排序规则](/character-set-and-collation.md)
14 changes: 7 additions & 7 deletions character-set-gbk.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ SHOW CHARACTER SET WHERE CHARSET = 'gbk';

```sql
SHOW COLLATION WHERE CHARSET = 'gbk';
```

```
+----------------+---------+----+---------+----------+---------+---------------+
| Collation | Charset | Id | Default | Compiled | Sortlen | Pad_attribute |
+----------------+---------+----+---------+----------+---------+---------------+
| gbk_bin | gbk | 87 | | Yes | 1 | PAD SPACE |
| gbk_chinese_ci | gbk | 28 | Yes | Yes | 1 | PAD SPACE |
+----------------+---------+----+---------+----------+---------+---------------+
2 rows in set (0.00 sec)
2 rows in set (0.001 sec)
```

## 与 MySQL 的兼容性
Expand All @@ -40,9 +37,12 @@ SHOW COLLATION WHERE CHARSET = 'gbk';

### 排序规则兼容性

MySQL 的字符集默认排序规则是 `gbk_chinese_ci`。与 MySQL 不同,TiDB GBK 字符集的默认排序规则为 `gbk_bin`。另外,TiDB 支持的 `gbk_bin` 与 MySQL 支持的 `gbk_bin` 排序规则也不一致,TiDB 是将 GBK 转换成 `utf8mb4`,然后再进行二进制排序。
MySQL 的 GBK 字符集默认排序规则是 `gbk_chinese_ci`。TiDB 的 GBK 字符集的默认排序规则取决于 TiDB 配置项[`new_collations_enabled_on_first_bootstrap`](/tidb-configuration-file.md#new_collations_enabled_on_first_bootstrap) 的值:

- 默认情况下,TiDB 配置项 [`new_collations_enabled_on_first_bootstrap`](/tidb-configuration-file.md#new_collations_enabled_on_first_bootstrap) 为 `true`,表示开启[新的排序规则框架](/character-set-and-collation.md#新框架下的排序规则支持)。GBK 字符集的默认排序规则是 `gbk_chinese_ci`。
- 当 TiDB 配置项 [`new_collations_enabled_on_first_bootstrap`](/tidb-configuration-file.md#new_collations_enabled_on_first_bootstrap) 为 `false` 时,表示关闭新的排序规则框架,GBK 字符集的默认排序规则是 `gbk_bin`。

如果要使 TiDB 兼容 MySQL 的 GBK 字符集排序规则,你需要在初次初始化 TiDB 集群时设置 TiDB 配置项 [`new_collations_enabled_on_first_bootstrap`](/tidb-configuration-file.md#new_collations_enabled_on_first_bootstrap) 为 `true` 来开启[新的排序规则框架](/character-set-and-collation.md#新框架下的排序规则支持)。对于新部署的系统,该设置是默认值
另外,TiDB 支持的 `gbk_bin` 与 MySQL 支持的 `gbk_bin` 排序规则也不一致,TiDB 是将 GBK 转换成 `utf8mb4`,然后再进行二进制排序

开启新的排序规则框架后,如果查看 GBK 字符集对应的排序规则,你可以看到 TiDB GBK 默认排序规则已经切换为 `gbk_chinese_ci`。

Expand Down Expand Up @@ -70,7 +70,7 @@ SHOW COLLATION WHERE CHARSET = 'gbk';
| gbk_bin | gbk | 87 | | Yes | 1 | PAD SPACE |
| gbk_chinese_ci | gbk | 28 | Yes | Yes | 1 | PAD SPACE |
+----------------+---------+----+---------+----------+---------+---------------+
2 rows in set (0.00 sec)
2 rows in set (0.001 sec)
```

### 非法字符兼容性
Expand Down
4 changes: 2 additions & 2 deletions dm/dm-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ tiup install dm dmctl

- DM 不会将视图的 DDL 语句同步到下游的 TiDB 集群,也不会将针对视图的 DML 语句同步到下游。在该场景下,建议用户在下游 TiDB 集群中自行创建视图。

+ GBK 字符集兼容性限制
+ GBK/GB18030 字符集兼容性限制

- DM 在 v5.4.0 之前不支持将 `charset=GBK` 的表迁移到 TiDB。
- DM 在 v5.4.0 之前不支持将 `charset=GBK` 的表迁移到 TiDB;当前 DM 不支持将 `charset=GB18030` 的表迁移到 TiDB

+ Binlog 兼容性限制

Expand Down
3 changes: 2 additions & 1 deletion information-schema/information-schema-character-sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ SELECT * FROM `CHARACTER_SETS`;
+--------------------+----------------------+-------------------------------------+--------+
| ascii | ascii_bin | US ASCII | 1 |
| binary | binary | binary | 1 |
| gb18030 | gb18030_chinese_ci | China National Standard GB18030 | 4 |
| gbk | gbk_chinese_ci | Chinese Internal Code Specification | 2 |
| latin1 | latin1_bin | Latin1 | 1 |
| utf8 | utf8_bin | UTF-8 Unicode | 3 |
| utf8mb4 | utf8mb4_bin | UTF-8 Unicode | 4 |
+--------------------+----------------------+-------------------------------------+--------+
6 rows in set (0.00 sec)
7 rows in set (0.00 sec)
```

`CHARACTER_SETS` 表中列的含义如下:
Expand Down
6 changes: 4 additions & 2 deletions migrate-from-mariadb.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ TiDB 不支持 MariaDB 中常用的 `latin1_swedish_ci` 排序规则。
SHOW COLLATION;
```

```
```sql
+--------------------+---------+-----+---------+----------+---------+---------------+
| Collation | Charset | Id | Default | Compiled | Sortlen | Pad_attribute |
+--------------------+---------+-----+---------+----------+---------+---------------+
| ascii_bin | ascii | 65 | Yes | Yes | 1 | PAD SPACE |
| binary | binary | 63 | Yes | Yes | 1 | NO PAD |
| gb18030_bin | gb18030 | 249 | | Yes | 1 | PAD SPACE |
| gb18030_chinese_ci | gb18030 | 248 | Yes | Yes | 1 | PAD SPACE |
| gbk_bin | gbk | 87 | | Yes | 1 | PAD SPACE |
| gbk_chinese_ci | gbk | 28 | Yes | Yes | 1 | PAD SPACE |
| latin1_bin | latin1 | 47 | Yes | Yes | 1 | PAD SPACE |
Expand All @@ -210,7 +212,7 @@ SHOW COLLATION;
| utf8mb4_general_ci | utf8mb4 | 45 | | Yes | 1 | PAD SPACE |
| utf8mb4_unicode_ci | utf8mb4 | 224 | | Yes | 8 | PAD SPACE |
+--------------------+---------+-----+---------+----------+---------+---------------+
13 rows in set (0.00 sec)
15 rows in set (0.000 sec)
```

执行下列语句检查当前表的列使用的排序规则:
Expand Down
Loading
Loading