8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

12.1 字符集和排序规则概述12.1 Character Sets and Collations in General

字符集(character set)是一组符号和编码。排序规则(collation)是一组用于比较字符集中的字符的规则。让我们通过一个虚构字符集的例子来明确区分。A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set. Let's make the distinction clear with an example of an imaginary character set.

假设我们有一个包含四个字母的字母表:A、B、a、b。我们给每个字母分配一个数字:A = 0,B = 1,a = 2,b = 3。字母A是一个符号,数字 0 是A的编码,所有四个字母及其编码的组合构成一个字符集。Suppose that we have an alphabet with four letters: A, B, a, b. We give each letter a number: A = 0, B = 1, a = 2, b = 3. The letter A is a symbol, the number 0 is the encoding for A, and the combination of all four letters and their encodings is a character set.

假设我们想要比较两个字符串值,A 和 B。最简单的方法是查看编码:A 为 0,B 为 1。因为 0 小于 1,我们说 A 小于 B。我们刚刚所做的是对我们的字符集应用排序规则。排序规则是一组规则(在此案例中只有一个规则):“比较编码。” 我们称这个最简单的排序规则为 二进制 排序规则。Suppose that we want to compare two string values, A and B. The simplest way to do this is to look at the encodings: 0 for A and 1 for B. Because 0 is less than 1, we say A is less than B. What we've just done is apply a collation to our character set. The collation is a set of rules (only one rule in this case): “compare the encodings.” We call this simplest of all possible collations a binary collation.

但是,如果我们想说小写字母和大写字母是等效的呢?那么我们至少需要两个规则:(1) 将小写字母 a 和 b 视为与 A 和 B 等效;(2) 然后比较编码。我们称这种排序为 不区分大小写 排序。它比二进制排序稍微复杂一些。But what if we want to say that the lowercase and uppercase letters are equivalent? Then we would have at least two rules: (1) treat the lowercase letters a and b as equivalent to A and B; (2) then compare the encodings. We call this a case-insensitive collation. It is a little more complex than a binary collation.

在现实生活中,大多数字符集包含许多字符:不仅仅是A和B,还有整个字母表,有时还有多个字母表或包含数千字符的东方书写系统,以及许多特殊符号和标点符号。同样在现实生活中,大多数排序规则也有许多规则,不仅仅是区分大小写,还包括是否区分重音符号(“accent”是附加在字符上的标记,如德语中的Ö)以及多字符映射(例如在两个德语排序规则之一中,Ö = OE的规则)。In real life, most character sets have many characters: not just A and B but whole alphabets, sometimes multiple alphabets or eastern writing systems with thousands of characters, along with many special symbols and punctuation marks. Also in real life, most collations have many rules, not just for whether to distinguish lettercase, but also for whether to distinguish accents (an “accent” is a mark attached to a character as in German Ö), and for multiple-character mappings (such as the rule that Ö = OE in one of the two German collations).

MySQL 可以为您做以下事情:MySQL can do these things for you:

  • 使用各种字符集存储字符串。Store strings using a variety of character sets.

  • 使用各种排序规则比较字符串。Compare strings using a variety of collations.

  • 在同一服务器、同一数据库,甚至同一表中混合使用不同字符集或排序规则的字符串。Mix strings with different character sets or collations in the same server, the same database, or even the same table.

  • 允许在任何级别指定字符集和排序规则。Enable specification of character set and collation at any level.

要有效使用这些功能,您必须了解可用的字符集和排序规则,如何更改默认值,以及它们如何影响字符串操作符和函数的行为。To use these features effectively, you must know what character sets and collations are available, how to change the defaults, and how they affect the behavior of string operators and functions.