文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 94%

12.9 Unicode 支持12.9 Unicode Support

Unicode 标准包括来自基本多语言平面(BMP)的字符以及位于 BMP 之外的补充字符。本节介绍了 MySQL 对 Unicode 的支持。有关 Unicode 标准本身的信息,请访问 Unicode 联盟网站。The Unicode Standard includes characters from the Basic Multilingual Plane (BMP) and supplementary characters that lie outside the BMP. This section describes support for Unicode in MySQL. For information about the Unicode Standard itself, visit the Unicode Consortium website.

BMP 字符具有以下特征:BMP characters have these characteristics:

  • 它们的代码点值在 0 到 65535 之间(或 U+0000 和 U+FFFF)。Their code point values are between 0 and 65535 (or U+0000 and U+FFFF).

  • 它们可以使用可变长度编码,用 8、16 或 24 位(1 到 3 个字节)进行编码。They can be encoded in a variable-length encoding using 8, 16, or 24 bits (1 to 3 bytes).

  • 它们可以使用固定长度编码,用 16 位(2 个字节)进行编码。They can be encoded in a fixed-length encoding using 16 bits (2 bytes).

  • 它们足以表示几乎所有主要语言的字符。They are sufficient for almost all characters in major languages.

补充字符位于基本多文种平面(BMP)之外:Supplementary characters lie outside the BMP:

  • 它们的代码点值在 U+10000 和 U+10FFFF 之间。Their code point values are between U+10000 and U+10FFFF).

  • 对补充字符的 Unicode 支持需要具有超出 BMP 字符范围的字符集,因此比 BMP 字符占用更多空间(每个字符最多 4 字节)。Unicode support for supplementary characters requires character sets that have a range outside BMP characters and therefore take more space than BMP characters (up to 4 bytes per character).

UTF-8(使用 8 位单元的 Unicode 转换格式)用于编码 Unicode 数据的方法是根据 RFC 3629 实现的,该 RFC 描述了使用一到四字节的编码序列。UTF-8 的思想是使用不同长度的字节序列来编码各种 Unicode 字符:The UTF-8 (Unicode Transformation Format with 8-bit units) method for encoding Unicode data is implemented according to RFC 3629, which describes encoding sequences that take from one to four bytes. The idea of UTF-8 is that various Unicode characters are encoded using byte sequences of different lengths:

  • 基本拉丁字母、数字和标点符号使用一个字节。Basic Latin letters, digits, and punctuation signs use one byte.

  • 大多数欧洲和中东文字使用 2 字节序列:扩展拉丁字母(带波浪号、上横线、尖音符、重音符等)、西里尔字母、希腊字母、亚美尼亚字母、希伯来字母、阿拉伯字母、叙利亚字母等。Most European and Middle East script letters fit into a 2-byte sequence: extended Latin letters (with tilde, macron, acute, grave and other accents), Cyrillic, Greek, Armenian, Hebrew, Arabic, Syriac, and others.

  • 韩文、中文和日文的表意文字使用 3 字节或 4 字节序列。Korean, Chinese, and Japanese ideographs use 3-byte or 4-byte sequences.

MySQL 支持以下 Unicode 字符集:MySQL supports these Unicode character sets:

  • utf8mb4:Unicode 字符集的 UTF-8 编码,每个字符使用一到四个字节。utf8mb4: A UTF-8 encoding of the Unicode character set using one to four bytes per character.

  • utf8mb3:Unicode 字符集的 UTF-8 编码,每个字符使用一到三个字节。此字符集已被弃用,并可能在以后的版本中移除;请改用 utf8mb4。utf8mb3: A UTF-8 encoding of the Unicode character set using one to three bytes per character. This character set is deprecated andsubject to removal in a future release; use utf8mb4 instead.

  • utf8:已被弃用的 utf8mb3 别名;请改用 utf8mb4。utf8: A deprecated alias for utf8mb3; use utf8mb4 instead.

    Note

    utf8 预计在未来的 MySQL 版本中将成为 utf8mb4 的别名。utf8 is expected in a future version of MySQL to become an alias for utf8mb4.

  • ucs2:Unicode 字符集的 UCS-2 编码,每个字符使用两个字节。已弃用;预计对该字符集的支持将在未来版本中移除。ucs2: The UCS-2 encoding of the Unicode character set using two bytes per character. Deprecated; expect support for this character set to be removed in a future release.

  • utf16:使用每个字符两个或四个字节的 UTF-16 编码表示 Unicode 字符集。类似 ucs2,但增加了对补充字符的扩展。utf16: The UTF-16 encoding for the Unicode character set using two or four bytes per character. Like ucs2 but with an extension for supplementary characters.

  • utf16le:Unicode 字符集的 UTF-16LE 编码。类似 utf16,但使用小端而非大端。utf16le: The UTF-16LE encoding for the Unicode character set. Like utf16 but little-endian rather than big-endian.

  • utf32:使用每个字符四个字节的 UTF-32 编码表示 Unicode 字符集。utf32: The UTF-32 encoding for the Unicode character set using four bytes per character.

Note

utf8mb3 字符集已被弃用,预计将在未来的 MySQL 版本中移除。请使用 utf8mb4。utf8 当前是 utf8mb3 的别名,但它现在也被弃用,预计 utf8 将来会成为 utf8mb4 的引用。在 MySQL 8.4 中,Information Schema 表的列以及 SQL SHOW 语句的输出中,也会显示 utf8mb3 代替 utf8。The utf8mb3 character set is deprecated and you should expect it to be removed in a future MySQL release. Please use utf8mb4 instead. utf8 is currently an alias for utf8mb3, but it is now deprecated as such, and utf8 is expected subsequently to become a reference to utf8mb4. MySQL 8.4 also displays utf8mb3 in place of utf8 in the columns of Information Schema tables, and in the output of SQL SHOW statements.

此外,你还应注意,在旧版本的 MySQL 中使用 utf8_ 前缀的排序规则已改名,改为使用 utf8mb3_ 前缀。In addition, you should be aware that collations using the utf8_ prefix in older releases of MySQL have since been renamed using the prefix utf8mb3_, instead.

为了避免 utf8 含义的歧义,建议在字符集引用中明确指定 utf8mb4。To avoid ambiguity about the meaning of utf8, consider specifying utf8mb4 explicitly for character set references.

表 12.2,“Unicode 字符集的一般特性” 总结了 MySQL 支持的 Unicode 字符集的一般特性。Table 12.2, “Unicode Character Set General Characteristics”, summarizes the general characteristics of Unicode character sets supported by MySQL.

表 12.2 Unicode 字符集的一般特性Table 12.2 Unicode Character Set General Characteristics

字符集Character Set 支持的字符Supported Characters 每个字符所需存储Required Storage Per Character
utf8mb3,utf8(已弃用)utf8mb3, utf8 (deprecated) 仅限 BMPBMP only 1、2 或 3 字节1, 2, or 3 bytes
ucs2 仅限 BMPBMP only 2 字节2 bytes
utf8mb4utf8mb4 BMP 和补充BMP and supplementary 1、2、3 或 4 字节1, 2, 3, or 4 bytes
utf16 BMP 和补充BMP and supplementary 2 或 4 字节2 or 4 bytes
utf16le BMP 和补充BMP and supplementary 2 或 4 字节2 or 4 bytes
utf32 BMP 和补充BMP and supplementary 4 字节4 bytes

BMP 以外的字符会被比较为 REPLACEMENT CHARACTER,并在转换为仅支持 BMP 字符的 Unicode 字符集(utf8mb3 或 ucs2)时转换为 '?'。Characters outside the BMP compare as REPLACEMENT CHARACTER and convert to '?' when converted to a Unicode character set that supports only BMP characters (utf8mb3 or ucs2).

If you use character sets that support supplementary characters and thus are “wider” than the BMP-only utf8mb3以及ucs2 character sets, there are potential incompatibility issues for your applications; see Section 12.9.8, “Converting Between 3-Byte and 4-Byte Unicode Character Sets”. That section also describes how to convert tables from the (3-byte) utf8mb3 to the (4-byte) utf8mb4, and what constraints may apply in doing so.If you use character sets that support supplementary characters and thus are “wider” than the BMP-only utf8mb3 and ucs2 character sets, there are potential incompatibility issues for your applications; see Section 12.9.8, “Converting Between 3-Byte and 4-Byte Unicode Character Sets”. That section also describes how to convert tables from the (3-byte) utf8mb3 to the (4-byte) utf8mb4, and what constraints may apply in doing so.

大多数 Unicode 字符集都提供类似的一组排序规则。例如,每个字符集都有丹麦语排序规则,其名称分别是 utf8mb4_danish_ci、utf8mb3_danish_ci(已弃用)、utf8_danish_ci(已弃用)、ucs2_danish_ci、utf16_danish_ci 和 utf32_danish_ci。例外的是 utf16le,它只有两种排序规则。有关 Unicode 排序规则及其差异化属性的信息,包括补充字符的排序规则属性,请参见 第 12.10.1 节,“Unicode 字符集”。A similar set of collations is available for most Unicode character sets. For example, each has a Danish collation, the names of which are utf8mb4_danish_ci, utf8mb3_danish_ci (deprecated), utf8_danish_ci (deprecated), ucs2_danish_ci, utf16_danish_ci, and utf32_danish_ci. The exception is utf16le, which has only two collations. For information about Unicode collations and their differentiating properties, including collation properties for supplementary characters, see Section 12.10.1, “Unicode Character Sets”.

MySQL 对 UCS-2、UTF-16 和 UTF-32 的实现以大端字节序存储字符,并且在值的开头不使用字节顺序标记(BOM)。其他数据库系统可能使用小端字节序或 BOM。在这种情况下,在这些系统与 MySQL 之间传输数据时需要进行值转换。UTF-16LE 的实现是小端字节序。The MySQL implementation of UCS-2, UTF-16, and UTF-32 stores characters in big-endian byte order and does not use a byte order mark (BOM) at the beginning of values. Other database systems might use little-endian byte order or a BOM. In such cases, conversion of values needs to be performed when transferring data between those systems and MySQL. The implementation of UTF-16LE is little-endian.

MySQL 对 UTF-8 值不使用 BOM。MySQL uses no BOM for UTF-8 values.

使用 Unicode 与服务器通信的客户端应用程序应相应地设置客户端字符集(例如,通过执行 SET NAMES 'utf8mb4' 语句)。某些字符集不能用作客户端字符集。尝试在 SET NAMES 或 SET CHARACTER SET 中使用它们会产生错误。详见 不允许的客户端字符集。Client applications that communicate with the server using Unicode should set the client character set accordingly (for example, by issuing a SET NAMES 'utf8mb4' statement). Some character sets cannot be used as the client character set. Attempting to use them with SET NAMES or SET CHARACTER SET produces an error. See Impermissible Client Character Sets.

以下各节提供了关于 MySQL 中 Unicode 字符集的更多详细信息。The following sections provide additional detail on the Unicode character sets in MySQL.