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

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

12.3.6 字符串字面量字符集和排序规则12.3.6 Character String Literal Character Set and Collation

每个字符字符串字面量都有一个字符集和排序规则。Every character string literal has a character set and a collation.

对于简单的语句 SELECT 'string',该字符串具有连接的默认字符集和排序规则,这些由 character_set_connection 和 collation_connection 系统变量定义。For the simple statement SELECT 'string', the string has the connection default character set and collation defined by the character_set_connection and collation_connection system variables.

字符字符串字面量可以有可选的字符集引入符和 COLLATE 子句,用于指定它为使用特定字符集和排序规则的字符串:A character string literal may have an optional character set introducer and COLLATE clause, to designate it as a string that uses a particular character set and collation:

[_charset_name]'string' [COLLATE collation_name]

_charset_name 表达式正式称为 引入符。它告诉解析器,“后续的字符串使用字符集 charset_name。”” 引入符不会像 CONVERT() 那样将字符串转换为引入符所指示的字符集。它不会改变字符串值,尽管可能会发生填充。引入符只是一个信号。参见 第 12.3.8 节,“字符集引入符”。The _charset_name expression is formally called an introducer. It tells the parser, “the string that follows uses character set charset_name.” An introducer does not change the string to the introducer character set like CONVERT() would do. It does not change the string value, although padding may occur. The introducer is just a signal. See Section 12.3.8, “Character Set Introducers”.

示例:Examples:

SELECT 'abc';
SELECT _latin1'abc';
SELECT _binary'abc';
SELECT _utf8mb4'abc' COLLATE utf8mb4_danish_ci;

字符集引入符和 COLLATE 子句是根据标准 SQL 规范实现的。Character set introducers and the COLLATE clause are implemented according to standard SQL specifications.

MySQL 以以下方式确定字符字符串字面量的字符集和排序规则:MySQL determines the character set and collation of a character string literal in the following manner:

  • 如果同时指定了_charset_name和COLLATE collation_name,则使用字符集charset_name和排序规则collation_name。collation_name必须是charset_name允许的排序规则。If both _charset_name and COLLATE collation_name are specified, character set charset_name and collation collation_name are used. collation_name must be a permitted collation for charset_name.

  • 如果指定了_charset_name,但未指定COLLATE,则使用字符集charset_name及其默认排序规则。要查看每个字符集的默认排序规则,请使用SHOW CHARACTER SET语句或查询INFORMATION_SCHEMA CHARACTER_SETS表。If _charset_name is specified but COLLATE is not specified, character set charset_name and its default collation are used. To see the default collation for each character set, use the SHOW CHARACTER SET statement or query the INFORMATION_SCHEMA CHARACTER_SETS table.

  • 如果未指定 _charset_name,但指定了 COLLATE collation_name,则使用由 character_set_connection 系统变量给出的连接默认字符集和排序规则 collation_name。collation_name 必须是连接默认字符集允许的排序规则。If _charset_name is not specified but COLLATE collation_name is specified, the connection default character set given by the character_set_connection system variable and collation collation_name are used. collation_name must be a permitted collation for the connection default character set.

  • 否则(既未指定 _charset_name,也未指定 COLLATE collation_name),将使用由 character_set_connection 和 collation_connection 系统变量给出的连接默认字符集和校对规则。Otherwise (neither _charset_name nor COLLATE collation_name is specified), the connection default character set and collation given by the character_set_connection and collation_connection system variables are used.

示例:Examples:

  • 一个使用 latin1 字符集和 latin1_german1_ci 校对规则的非二进制字符串:A nonbinary string with latin1 character set and latin1_german1_ci collation:

    SELECT _latin1'Müller' COLLATE latin1_german1_ci;
    
  • 一个使用 utf8mb4 字符集及其默认校对规则(即 utf8mb4_0900_ai_ci)的非二进制字符串:A nonbinary string with utf8mb4 character set and its default collation (that is, utf8mb4_0900_ai_ci):

    SELECT _utf8mb4'Müller';
    
  • 具有 binary 字符集及其默认排序规则(即 binary)的二进制字符串:A binary string with binary character set and its default collation (that is, binary):

    SELECT _binary'Müller';
    
  • 具有连接默认字符集和 utf8mb4_0900_ai_ci 排序规则的非二进制字符串(如果连接字符集不是 utf8mb4 则失败):A nonbinary string with the connection default character set and utf8mb4_0900_ai_ci collation (fails if the connection character set is not utf8mb4):

    SELECT 'Müller' COLLATE utf8mb4_0900_ai_ci;
    
  • 包含连接默认字符集和校对规则的字符串:A string with the connection default character set and collation:

    SELECT 'Müller';
    

引导符指示后续字符串的字符集,但不会改变解析器在字符串中执行转义处理的方式。转义总是由解析器根据character_set_connection指定的字符集来解释。An introducer indicates the character set for the following string, but does not change how the parser performs escape processing within the string. Escapes are always interpreted by the parser according to the character set given by character_set_connection.

以下示例显示,即使存在引导符,也会使用 character_set_connection 进行转义处理。示例使用 SET NAMES(它会更改 character_set_connection,如 第 12.4 节,“连接字符集和排序规则” 所述),并使用 HEX() 函数显示结果字符串,以便可以查看字符串的确切内容。The following examples show that escape processing occurs using character_set_connection even in the presence of an introducer. The examples use SET NAMES (which changes character_set_connection, as discussed in Section 12.4, “Connection Character Sets and Collations”), and display the resulting strings using the HEX() function so that the exact string contents can be seen.

示例 1:Example 1:

mysql> SET NAMES latin1;
mysql> SELECT HEX('à\n'), HEX(_sjis'à\n');
+------------+-----------------+
| HEX('à\n')  | HEX(_sjis'à\n')  |
+------------+-----------------+
| E00A       | E00A            |
+------------+-----------------+

这里,à(十六进制值 E0)后跟 \n,即换行的转义序列。该转义序列使用 character_set_connection 的 latin1 值进行解释,从而生成字面上的换行符(十六进制值 0A)。即使是第二个字符串也是如此。也就是说,_sjis 引入符不会影响解析器的转义处理。Here, à (hexadecimal value E0) is followed by \n, the escape sequence for newline. The escape sequence is interpreted using the character_set_connection value of latin1 to produce a literal newline (hexadecimal value 0A). This happens even for the second string. That is, the _sjis introducer does not affect the parser's escape processing.

示例 2:Example 2:

mysql> SET NAMES sjis;
mysql> SELECT HEX('à\n'), HEX(_latin1'à\n');
+------------+-------------------+
| HEX('à\n')  | HEX(_latin1'à\n')  |
+------------+-------------------+
| E05C6E     | E05C6E            |
+------------+-------------------+

这里,character_set_connection 是 sjis,一种字符集,其中序列 à 后跟 \(十六进制值 05 和 5C)是一个有效的多字节字符。因此,字符串的前两个字节被解释为单个 sjis 字符,而 \ 不被解释为转义字符。接下来的 n(十六进制值 6E)不被解释为转义序列的一部分。即使对于第二个字符串也是如此;_latin1 前缀不会影响转义处理。Here, character_set_connection is sjis, a character set in which the sequence of à followed by \ (hexadecimal values 05 and 5C) is a valid multibyte character. Hence, the first two bytes of the string are interpreted as a single sjis character, and the \ is not interpreted as an escape character. The following n (hexadecimal value 6E) is not interpreted as part of an escape sequence. This is true even for the second string; the _latin1 introducer does not affect escape processing.