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

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

MySQL 8.4 参考手册 / 12 种字符集、排序规则、Unicode / 12.8 整理问题 / 12.8.5 二进制排序规则与 _bin 排序规则的比较
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

12.8.5 二进制排序规则与 _bin 排序规则比较12.8.5 The binary Collation Compared to _bin Collations

本节描述了binary二进制字符串的排序规则如何与_bin非二进制字符串的排序规则进行比较。This section describes how the binary collation for binary strings compares to _bin collations for nonbinary strings.

二进制字符串(使用BINARY, VARBINARY,以及BLOB数据类型存储)具有名为binary的字符集和排序规则。二进制字符串是字节序列,并且这些字节的数值决定了比较和排序顺序。参见第 12.10.8 节,“二进制字符集”.Binary strings (as stored using the BINARY, VARBINARY, and BLOB data types) have a character set and collation named binary. Binary strings are sequences of bytes and the numeric values of those bytes determine comparison and sort order. See Section 12.10.8, “The Binary Character Set”.

非二进制字符串(以 CHAR、VARCHAR 和 TEXT 数据类型存储)具有非 binary 的字符集和排序规则。特定的非二进制字符集可以有多种排序规则,每种排序规则都定义了该字符集中字符的特定比较和排序顺序。对于大多数字符集,其中一种是二进制排序规则,在排序规则名称中以 _bin 后缀表示。例如,latin1 和 big5 的二进制排序规则分别命名为 latin1_bin 和 big5_bin。utf8mb4 是一个例外,它有两种二进制排序规则,分别为 utf8mb4_bin 和 utf8mb4_0900_bin;参见 第 12.10.1 节,“Unicode 字符集”。Nonbinary strings (as stored using the CHAR, VARCHAR, and TEXT data types) have a character set and collation other than binary. A given nonbinary character set can have several collations, each of which defines a particular comparison and sort order for the characters in the set. For most character sets, one of these is the binary collation, indicated by a _bin suffix in the collation name. For example, the binary collations for latin1 and big5 are named latin1_bin and big5_bin, respectively. utf8mb4 is an exception that has two binary collations, utf8mb4_bin and utf8mb4_0900_bin; see Section 12.10.1, “Unicode Character Sets”.

binary排序规则在多个方面与 _bin 排序规则不同,具体内容在以下章节中讨论:The binary collation differs from _bin collations in several respects, discussed in the following sections:

比较和排序的单位The Unit for Comparison and Sorting

二进制字符串是字节序列。对于binary 排序规则,比较和排序基于数字字节值。非二进制字符串是字符序列,可能为多字节。非二进制字符串的排序规则定义了字符值的比较和排序顺序。对于_bin 排序规则,这种顺序基于数字字符码值,这与二进制字符串的排序类似,只是字符码值可能为多字节。Binary strings are sequences of bytes. For the binary collation, comparison and sorting are based on numeric byte values. Nonbinary strings are sequences of characters, which might be multibyte. Collations for nonbinary strings define an ordering of the character values for comparison and sorting. For _bin collations, this ordering is based on numeric character code values, which is similar to ordering for binary strings except that character code values might be multibyte.

字符集转换Character Set Conversion

一个非二进制字符串有一个字符集,并且在许多情况下会自动转换为另一种字符集,即使该字符串有一个 _bin 校对规则:A nonbinary string has a character set and is automatically converted to another character set in many cases, even when the string has a _bin collation:

  • 在将列值分配给具有不同字符集的另一列时:When assigning column values to another column that has a different character set:

    UPDATE t1 SET utf8mb4_bin_column=latin1_column;
    INSERT INTO t1 (latin1_column) SELECT utf8mb4_bin_column FROM t2;
    
  • 在使用字符串字面值为 INSERT 或 UPDATE 分配列值时:When assigning column values for INSERT or UPDATE using a string literal:

    SET NAMES latin1;
    INSERT INTO t1 (utf8mb4_bin_column) VALUES ('string-in-latin1');
    
  • 当从服务器向客户端发送结果时:When sending results from the server to a client:

    SET NAMES latin1;
    SELECT utf8mb4_bin_column FROM t2;
    

对于二进制字符串列,不进行任何转换。对于类似前述情况的,字符串值按字节复制。For binary string columns, no conversion occurs. For cases similar to those preceding, the string value is copied byte-wise.

字母大小写转换Lettercase Conversion

非二进制字符集的排序规则提供字符的大小写信息,因此非二进制字符串中的字符可以从一种字母大小写转换为另一种字母大小写,即使对于忽略排序中字母大小写的 _bin 排序规则也是如此:Collations for nonbinary character sets provide information about lettercase of characters, so characters in a nonbinary string can be converted from one lettercase to another, even for _bin collations that ignore lettercase for ordering:

mysql> SET NAMES utf8mb4 COLLATE utf8mb4_bin;
mysql> SELECT LOWER('aA'), UPPER('zZ');
+-------------+-------------+
| LOWER('aA') | UPPER('zZ') |
+-------------+-------------+
| aa          | ZZ          |
+-------------+-------------+

字母大小写的概念不适用于二进制字符串中的字节。要进行字母大小写转换,必须首先使用适合存储在字符串中的数据的字符集将字符串转换为非二进制字符串:The concept of lettercase does not apply to bytes in a binary string. To perform lettercase conversion, the string must first be converted to a nonbinary string using a character set appropriate for the data stored in the string:

mysql> SET NAMES binary;
mysql> SELECT LOWER('aA'), LOWER(CONVERT('aA' USING utf8mb4));
+-------------+------------------------------------+
| LOWER('aA') | LOWER(CONVERT('aA' USING utf8mb4)) |
+-------------+------------------------------------+
| aA          | aa                                 |
+-------------+------------------------------------+

比较中的尾随空格处理Trailing Space Handling in Comparisons

MySQL 排序规则具有 pad 属性,其值为 PAD SPACE 或 NO PAD:MySQL collations have a pad attribute, which has a value of PAD SPACE or NO PAD:

对于非二进制字符串(CHAR、VARCHAR 和 TEXT 值),字符串排序规则的 pad 属性决定了在比较字符串末尾尾随空格时的处理方式:For nonbinary strings (CHAR, VARCHAR, and TEXT values), the string collation pad attribute determines treatment in comparisons of trailing spaces at the end of strings:

  • 对于 PAD SPACE 排序规则,在比较时尾随空格不重要;字符串比较时不考虑尾随空格。For PAD SPACE collations, trailing spaces are insignificant in comparisons; strings are compared without regard to trailing spaces.

  • NO PAD 排序规则在比较时将尾随空格视为重要,就像其他字符一样。NO PAD collations treat trailing spaces as significant in comparisons, like any other character.

不同的行为可以通过使用两个 utf8mb4 二进制排序规则来演示,其中一个是 PAD SPACE,另一个是 NO PAD。该示例还展示了如何使用 INFORMATION_SCHEMA COLLATIONS 表来确定排序规则的填充属性。The differing behaviors can be demonstrated using the two utf8mb4 binary collations, one of which is PAD SPACE, the other of which is NO PAD. The example also shows how to use the INFORMATION_SCHEMA COLLATIONS table to determine the pad attribute for collations.

mysql> SELECT COLLATION_NAME, PAD_ATTRIBUTE
       FROM INFORMATION_SCHEMA.COLLATIONS
       WHERE COLLATION_NAME LIKE 'utf8mb4%bin';
+------------------+---------------+
| COLLATION_NAME   | PAD_ATTRIBUTE |
+------------------+---------------+
| utf8mb4_bin      | PAD SPACE     |
| utf8mb4_0900_bin | NO PAD        |
+------------------+---------------+
mysql> SET NAMES utf8mb4 COLLATE utf8mb4_bin;
mysql> SELECT 'a ' = 'a';
+------------+
| 'a ' = 'a' |
+------------+
|          1 |
+------------+
mysql> SET NAMES utf8mb4 COLLATE utf8mb4_0900_bin;
mysql> SELECT 'a ' = 'a';
+------------+
| 'a ' = 'a' |
+------------+
|          0 |
+------------+
Note

“比较” 在此上下文中不包括 LIKE 模式匹配操作符,对于该操作符,末尾空格是重要的,无论排序规则如何。“Comparison” in this context does not include the LIKE pattern-matching operator, for which trailing spaces are significant, regardless of collation.

对于二进制字符串(BINARY、VARBINARY 和 BLOB 值),比较时所有字节都是重要的,包括末尾空格:For binary strings (BINARY, VARBINARY, and BLOB values), all bytes are significant in comparisons, including trailing spaces:

mysql> SET NAMES binary;
mysql> SELECT 'a ' = 'a';
+------------+
| 'a ' = 'a' |
+------------+
|          0 |
+------------+

插入和检索的末尾空格处理Trailing Space Handling for Inserts and Retrievals

CHAR(N) 列存储长度为 N 个字符的非二进制字符串。对于插入操作,长度短于 N 个字符的值将用空格进行扩展。对于检索操作,会移除尾随空格。CHAR(N) columns store nonbinary strings N characters long. For inserts, values shorter than N characters are extended with spaces. For retrievals, trailing spaces are removed.

BINARY(N) 列存储长度为 N 个字节的二进制字符串。对于插入操作,长度短于 N 个字节的值将用 0x00 字节进行扩展。对于检索操作,不会移除任何内容;始终返回声明长度的值。BINARY(N) columns store binary strings N bytes long. For inserts, values shorter than N bytes are extended with 0x00 bytes. For retrievals, nothing is removed; a value of the declared length is always returned.

mysql> CREATE TABLE t1 (
         a CHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
         b BINARY(10)
       );
mysql> INSERT INTO t1 VALUES ('x','x');
mysql> INSERT INTO t1 VALUES ('x ','x ');
mysql> SELECT a, b, HEX(a), HEX(b) FROM t1;
+------+------------------------+--------+----------------------+
| a    | b                      | HEX(a) | HEX(b)               |
+------+------------------------+--------+----------------------+
| x    | 0x78000000000000000000 | 78     | 78000000000000000000 |
| x    | 0x78200000000000000000 | 78     | 78200000000000000000 |
+------+------------------------+--------+----------------------+