8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

MySQL 8.4 参考手册 / 12 种字符集、排序规则、Unicode / 12.14 向字符集中添加排序规则
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

12.14 向字符集添加排序规则12.14 Adding a Collation to a Character Set

Warning

用户定义的排序规则已被弃用;您应预期它们的支持将在未来的 MySQL 版本中被移除。MySQL 8.4 服务器对于在 SQL 语句中使用 COLLATE user_defined_collation 会发出警告;当服务器启动时,如果 --collation-server 设置为用户定义的排序规则名称,也会发出警告。User-defined collations are deprecated; you should expect support for them to be removed in a future version of MySQL. The MySQL 8.4 server issues a warning for any use of COLLATE user_defined_collation in an SQL statement; a warning is also issued when the server is started with --collation-server set equal to the name of a user-defined collation.

排序规则是一组定义如何比较和排序字符字符串的规则。MySQL 中的每个排序规则都属于单个字符集。每个字符集至少有一个排序规则,大多数字符集有两个或更多的排序规则。A collation is a set of rules that defines how to compare and sort character strings. Each collation in MySQL belongs to a single character set. Every character set has at least one collation, and most have two or more collations.

排序规则根据权重对字符进行排序。字符集中的每个字符都映射到一个权重。权重相等的字符比较结果为相等,权重不相等的字符根据其权重的相对大小进行比较。A collation orders characters based on weights. Each character in a character set maps to a weight. Characters with equal weights compare as equal, and characters with unequal weights compare according to the relative magnitude of their weights.

WEIGHT_STRING() 函数可用于查看字符串中字符的权重。它返回的用于表示权重的值是二进制字符串,因此使用 HEX(WEIGHT_STRING(str)) 来以可打印形式显示权重是很方便的。下面的示例表明,对于非二进制不区分大小写的字符串,'AaBb' 中字母的权重不会因大小写而异,但如果是二进制字符串,则会不同:The WEIGHT_STRING() function can be used to see the weights for the characters in a string. The value that it returns to indicate weights is a binary string, so it is convenient to use HEX(WEIGHT_STRING(str)) to display the weights in printable form. The following example shows that weights do not differ for lettercase for the letters in 'AaBb' if it is a nonbinary case-insensitive string, but do differ if it is a binary string:

mysql> SELECT HEX(WEIGHT_STRING('AaBb' COLLATE latin1_swedish_ci));
+------------------------------------------------------+
| HEX(WEIGHT_STRING('AaBb' COLLATE latin1_swedish_ci)) |
+------------------------------------------------------+
| 41414242                                             |
+------------------------------------------------------+
mysql> SELECT HEX(WEIGHT_STRING(BINARY 'AaBb'));
+-----------------------------------+
| HEX(WEIGHT_STRING(BINARY 'AaBb')) |
+-----------------------------------+
| 41614262                          |
+-----------------------------------+

MySQL 支持几种排序规则实现,如第 12.14.1 节,“排序实现类型”所讨论的。其中一些可以在不重新编译 MySQL 的情况下添加:MySQL supports several collation implementations, as discussed in Section 12.14.1, “Collation Implementation Types”. Some of these can be added to MySQL without recompiling:

  • 用于 8 位字符集的简单排序规则。Simple collations for 8-bit character sets.

  • 基于 UCA 的 Unicode 字符集排序规则。UCA-based collations for Unicode character sets.

  • 二进制 (xxx_bin) 排序规则。Binary (xxx_bin) collations.

以下部分描述了如何向现有字符集添加前两种类型的用户定义排序规则。所有现有字符集已经具有二进制排序规则,因此这里无需描述如何添加二进制排序规则。The following sections describe how to add user-defined collations of the first two types to existing character sets. All existing character sets already have a binary collation, so there is no need here to describe how to add one.

Warning

不支持重新定义内置排序规则,这可能导致服务器行为异常。Redefining built-in collations is not supported and may result in unexpected server behavior.

添加新的用户定义排序规则的步骤概述:Summary of the procedure for adding a new user-defined collation:

  1. 选择一个排序规则 ID。Choose a collation ID.

  2. 添加命名排序规则并描述字符排序规则的配置信息。Add configuration information that names the collation and describes the character-ordering rules.

  3. 重启服务器。Restart the server.

  4. 验证服务器是否识别该排序规则。Verify that the server recognizes the collation.

此处的说明仅涵盖可在不重新编译 MySQL 的情况下添加的用户定义排序规则。若要添加需要重新编译的排序规则(通过 C 源文件中的函数实现),请使用 第 12.13 节,“添加字符集” 中的说明。不过,不是添加完整字符集所需的所有信息,而是只修改现有字符集的相应文件。也就是说,基于字符集当前排序规则已有的内容,为新的排序规则添加数据结构、函数和配置信息。The instructions here cover only user-defined collations that can be added without recompiling MySQL. To add a collation that does require recompiling (as implemented by means of functions in a C source file), use the instructions in Section 12.13, “Adding a Character Set”. However, instead of adding all the information required for a complete character set, just modify the appropriate files for an existing character set. That is, based on what is already present for the character set's current collations, add data structures, functions, and configuration information for the new collation.

Note

如果您修改现有的用户定义排序规则,这可能会影响对使用该排序规则的列上索引的行顺序。在这种情况下,请重建任何此类索引以避免出现错误的查询结果等问题。请参阅 第 3.14 节,“重建或修复表或索引”。If you modify an existing user-defined collation, that may affect the ordering of rows for indexes on columns that use the collation. In this case, rebuild any such indexes to avoid problems such as incorrect query results. See Section 3.14, “Rebuilding or Repairing Tables or Indexes”.

附加资源Additional Resources