每个字符字符串字面量都有一个字符集和排序规则。Every character string literal has a character set and a collation.
对于简单的语句 SELECT
'string',该字符串具有连接的默认字符集和排序规则,这些由 character_set_connection 和 collation_connection 系统变量定义。For the simple statement SELECT
', the string has
the connection default character set and collation defined by
the string'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' [COLLATEcollation_name]
_ 表达式正式称为 引入符。它告诉解析器,“后续的字符串使用字符集 charset_namecharset_name。”” 引入符不会像 CONVERT() 那样将字符串转换为引入符所指示的字符集。它不会改变字符串值,尽管可能会发生填充。引入符只是一个信号。参见 第 12.3.8 节,“字符集引入符”。The _
expression is formally called an
introducer. It tells the parser, “the
string that follows uses character set
charset_namecharset_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_nameandCOLLATEare specified, character setcollation_namecharset_nameand collationcollation_nameare used.collation_namemust be a permitted collation forcharset_name.如果指定了
_charset_name,但未指定COLLATE,则使用字符集charset_name及其默认排序规则。要查看每个字符集的默认排序规则,请使用SHOW CHARACTER SET语句或查询INFORMATION_SCHEMACHARACTER_SETS表。If_charset_nameis specified butCOLLATEis not specified, character setcharset_nameand its default collation are used. To see the default collation for each character set, use theSHOW CHARACTER SETstatement or query theINFORMATION_SCHEMACHARACTER_SETStable.如果未指定
_charset_name,但指定了COLLATE collation_name,则使用由character_set_connection系统变量给出的连接默认字符集和排序规则collation_name。collation_name必须是连接默认字符集允许的排序规则。If_charset_nameis not specified butCOLLATEis specified, the connection default character set given by thecollation_namecharacter_set_connectionsystem variable and collationcollation_nameare used.collation_namemust be a permitted collation for the connection default character set.否则(既未指定
_charset_name,也未指定COLLATE collation_name),将使用由character_set_connection和collation_connection系统变量给出的连接默认字符集和校对规则。Otherwise (neither_charset_namenorCOLLATEis specified), the connection default character set and collation given by thecollation_namecharacter_set_connectionandcollation_connectionsystem variables are used.
示例:Examples:
一个使用
latin1字符集和latin1_german1_ci校对规则的非二进制字符串:A nonbinary string withlatin1character set andlatin1_german1_cicollation:SELECT _latin1'Müller' COLLATE latin1_german1_ci;
一个使用
utf8mb4字符集及其默认校对规则(即utf8mb4_0900_ai_ci)的非二进制字符串:A nonbinary string withutf8mb4character set and its default collation (that is,utf8mb4_0900_ai_ci):SELECT _utf8mb4'Müller';
具有
binary字符集及其默认排序规则(即binary)的二进制字符串:A binary string withbinarycharacter 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 andutf8mb4_0900_ai_cicollation (fails if the connection character set is notutf8mb4):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.