8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

11.1.1 字符串字面值11.1.1 String Literals

字符串是字节或字符的序列,用单引号 (') 或双引号 (") 括起来。示例:A string is a sequence of bytes or characters, enclosed within either single quote (') or double quote (") characters. Examples:

'a string'
"another string"

将相邻放置的引号字符串连接成一个字符串。以下各行是等效的:Quoted strings placed next to each other are concatenated to a single string. The following lines are equivalent:

'a string'
'a' ' ' 'string'

如果启用了 ANSI_QUOTES SQL 模式,字符串字面量只能用单引号引用,因为用双引号引用的字符串会被解释为标识符。If the ANSI_QUOTES SQL mode is enabled, string literals can be quoted only within single quotation marks because a string quoted within double quotation marks is interpreted as an identifier.

二进制字符串 是字节字符串。每个二进制字符串都有一个名为 binary 的字符集和校对规则。非二进制字符串 是字符字符串。它有一个非 binary 的字符集和与该字符集兼容的校对规则。A binary string is a string of bytes. Every binary string has a character set and collation named binary. A nonbinary string is a string of characters. It has a character set other than binary and a collation that is compatible with the character set.

对于这两种类型的字符串,比较是基于字符串单元的数值。对于二进制字符串,单元是字节;比较使用数字字节值。对于非二进制字符串,单元是字符,并且一些字符集支持多字节字符;比较使用数字字符编码值。字符编码排序是字符串排序规则的函数。(有关更多信息,请参见第 12.8.5 节,“二进制排序规则与_bin 排序规则的比较”。)For both types of strings, comparisons are based on the numeric values of the string unit. For binary strings, the unit is the byte; comparisons use numeric byte values. For nonbinary strings, the unit is the character and some character sets support multibyte characters; comparisons use numeric character code values. Character code ordering is a function of the string collation. (For more information, see Section 12.8.5, “The binary Collation Compared to _bin Collations”.)

Note

在 mysql 客户端中,二进制字符串显示为十六进制表示,具体取决于 --binary-as-hex 的值。有关该选项的更多信息,请参阅 第 6.5.1 节,“mysql — MySQL 命令行客户端”。Within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex. For more information about that option, see Section 6.5.1, “mysql — The MySQL Command-Line Client”.

字符字符串字面量可以有可选的字符集引入符和 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]

示例:Examples:

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

您可以使用 N'literal'(或 n'literal')来创建国家字符集中的字符串。这些语句是等价的:You can use N'literal' (or n'literal') to create a string in the national character set. These statements are equivalent:

SELECT N'some text';
SELECT n'some text';
SELECT _utf8'some text';

有关这些字符串语法形式的信息,请参见 第 12.3.7 节,“国家字符集” 和 第 12.3.8 节,“字符集引入符”。For information about these forms of string syntax, see Section 12.3.7, “The National Character Set”, and Section 12.3.8, “Character Set Introducers”.

在字符串中,某些序列具有特殊含义,除非启用了 NO_BACKSLASH_ESCAPES SQL 模式。这些序列中的每一个都以反斜杠 (\) 开头,称为 转义字符。MySQL 识别 表 11.1,“特殊字符转义序列” 中显示的转义序列。对于所有其他转义序列,反斜杠会被忽略。也就是说,转义字符会被解释为未转义的字符。例如,\x 只是 x。这些序列区分大小写。例如,\b 被解释为退格符,而 \B 被解释为 B。转义处理是根据 character_set_connection 系统变量指示的字符集进行的。即使对于前面带有指示不同字符集的引导器的字符串,也适用此规则,如 第 12.3.6 节,“字符字符串字面值字符集和排序规则” 中所讨论的。Within a string, certain sequences have special meaning unless the NO_BACKSLASH_ESCAPES SQL mode is enabled. Each of these sequences begins with a backslash (\), known as the escape character. MySQL recognizes the escape sequences shown in Table 11.1, “Special Character Escape Sequences”. For all other escape sequences, backslash is ignored. That is, the escaped character is interpreted as if it was not escaped. For example, \x is just x. These sequences are case-sensitive. For example, \b is interpreted as a backspace, but \B is interpreted as B. Escape processing is done according to the character set indicated by the character_set_connection system variable. This is true even for strings that are preceded by an introducer that indicates a different character set, as discussed in Section 12.3.6, “Character String Literal Character Set and Collation”.

表 11.1 特殊字符转义序列Table 11.1 Special Character Escape Sequences

转义序列Escape Sequence 序列表示的字符Character Represented by Sequence
\0 一个 ASCII NUL (X'00') 字符An ASCII NUL (X'00') character
\' 一个单引号 (') 字符A single quote (') character
\" 一个双引号 (") 字符A double quote (") character
\b 一个退格字符A backspace character
\n 换行符(换行)字符A newline (linefeed) character
\r 回车字符A carriage return character
\t 一个制表符A tab character
\Z ASCII 26(Control+Z);见表格后的注释ASCII 26 (Control+Z); see note following the table
\\ 反斜杠 (\) 字符A backslash (\) character
\% % 字符;参见表格后的说明A % character; see note following the table
\_ _ 字符;参见表格后的说明A _ character; see note following the table

ASCII 26 字符可以编码为 \Z,以便让你绕过在 Windows 上 ASCII 26 表示文件结束 (END-OF-FILE) 的问题。如果尝试使用 mysql db_name < file_name,文件中的 ASCII 26 会导致问题。The ASCII 26 character can be encoded as \Z to enable you to work around the problem that ASCII 26 stands for END-OF-FILE on Windows. ASCII 26 within a file causes problems if you try to use mysql db_name < file_name.

\% 和 \_ 序列用于在模式匹配的上下文中搜索 % 和 _ 的字面实例,否则它们将被解释为通配符字符。请参阅 LIKE 运算符在 第 14.8.1 节,“字符串比较函数和运算符” 中的描述。如果在非模式匹配的上下文中使用 \% 或 \_,它们会计算为字符串 \% 和 \_,而不是 % 和 _。The \% and \_ sequences are used to search for literal instances of % and _ in pattern-matching contexts where they would otherwise be interpreted as wildcard characters. See the description of the LIKE operator in Section 14.8.1, “String Comparison Functions and Operators”. If you use \% or \_ outside of pattern-matching contexts, they evaluate to the strings \% and \_, not to % and _.

在字符串中包含引号字符有几种方法:There are several ways to include quote characters within a string:

  • 用 ' 引起来的字符串内部的 ' 可以写成 ''。A ' inside a string quoted with ' may be written as ''.

  • 用 " 引起来的字符串内部的 " 可以写成 ""。A " inside a string quoted with " may be written as "".

  • 在引号字符前加上转义字符 (\)。Precede the quote character by an escape character (\).

  • 用 " 引起来的字符串内部的 ' 不需要特殊处理,无需重复或转义。同样地,用 ' 引起来的字符串内部的 " 也不需要特殊处理。A ' inside a string quoted with " needs no special treatment and need not be doubled or escaped. In the same way, " inside a string quoted with ' needs no special treatment.

下面的 SELECT 语句演示了引号和转义的使用方法:The following SELECT statements demonstrate how quoting and escaping work:

mysql> SELECT 'hello', '"hello"', '""hello""', 'hel''lo', '\'hello';
+-------+---------+-----------+--------+--------+
| hello | "hello" | ""hello"" | hel'lo | 'hello |
+-------+---------+-----------+--------+--------+

mysql> SELECT "hello", "'hello'", "''hello''", "hel""lo", "\"hello";
+-------+---------+-----------+--------+--------+
| hello | 'hello' | ''hello'' | hel"lo | "hello |
+-------+---------+-----------+--------+--------+

mysql> SELECT 'This\nIs\nFour\nLines';
+--------------------+
| This
Is
Four
Lines |
+--------------------+

mysql> SELECT 'disappearing\ backslash';
+------------------------+
| disappearing backslash |
+------------------------+

要将二进制数据插入字符串列(例如BLOB 列),应使用转义序列表示某些字符。反斜杠 (\) 和用于引用字符串的引号字符必须转义。在某些客户端环境中,还可能需要转义 NUL 或 Control Z。如果没有转义,mysql 客户端会截断包含 NUL 字符的引用字符串,并且在 Windows 上如果不转义,Control Z 可能被视为文件结束符。有关表示这些字符的转义序列,请参见 表 11.1,“特殊字符转义序列”。To insert binary data into a string column (such as a BLOB column), you should represent certain characters by escape sequences. Backslash (\) and the quote character used to quote the string must be escaped. In certain client environments, it may also be necessary to escape NUL or Control+Z. The mysql client truncates quoted strings containing NUL characters if they are not escaped, and Control+Z may be taken for END-OF-FILE on Windows if not escaped. For the escape sequences that represent each of these characters, see Table 11.1, “Special Character Escape Sequences”.

在编写应用程序时,任何可能包含这些特殊字符的字符串在作为发送到 MySQL 服务器的 SQL 语句中的数据值使用之前,必须进行适当的转义。您可以通过两种方式来实现:When writing application programs, any string that might contain any of these special characters must be properly escaped before the string is used as a data value in an SQL statement that is sent to the MySQL server. You can do this in two ways:

  • 使用一个能够转义特殊字符的函数来处理字符串。在 C 程序中,可以使用 mysql_real_escape_string_quote() C API 函数来转义字符。请参见 mysql_real_escape_string_quote()。在构建其他 SQL 语句的 SQL 语句中,可以使用 QUOTE() 函数。Perl DBI 接口提供了一个 quote 方法,将特殊字符转换为适当的转义序列。请参见 第 31.9 节,“MySQL Perl API”。其它语言接口也可能提供类似功能。Process the string with a function that escapes the special characters. In a C program, you can use the mysql_real_escape_string_quote() C API function to escape characters. See mysql_real_escape_string_quote(). Within SQL statements that construct other SQL statements, you can use the QUOTE() function. The Perl DBI interface provides a quote method to convert special characters to the proper escape sequences. See Section 31.9, “MySQL Perl API”. Other language interfaces may provide a similar capability.

  • 作为显式转义特殊字符的替代方法,许多 MySQL API 提供了占位符功能,使您能够在语句字符串中插入特殊标记,然后在执行该语句时将数据值绑定到这些标记上。在这种情况下,API 会为您处理值中的特殊字符转义。As an alternative to explicitly escaping special characters, many MySQL APIs provide a placeholder capability that enables you to insert special markers into a statement string, and then bind data values to them when you issue the statement. In this case, the API takes care of escaping special characters in the values for you.