Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, resource group and other object names are known as identifiers. MySQL中的某些对象,包括数据库、表、索引、列、别名、视图、存储过程、分区、表空间、资源组和其他对象名称,都被称为标识符。This section describes the permissible syntax for identifiers in MySQL. 本节介绍MySQL中允许的标识符语法。Section 9.2.1, “Identifier Length Limits”, indicates the maximum length of each type of identifier. 第9.2.1节,“标识符长度限制”指出了每种标识符的最大长度。Section 9.2.3, “Identifier Case Sensitivity”, describes which types of identifiers are case-sensitive and under what conditions.第9.2.3节,“标识符区分大小写”描述了哪些类型的标识符是区分大小写的,以及在什么条件下。
An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. 标识符可以被引用也可以不被引用。如果标识符包含特殊字符或是保留字,则在引用它时必须引用它。(Exception: A reserved word that follows a period in a qualified name must be an identifier, so it need not be quoted.) Reserved words are listed at Section 9.3, “Keywords and Reserved Words”.(例外:限定名称中句点后面的保留字必须是标识符,因此无需引用。)第9.3节,“关键字和保留字”列出了保留字。
Internally, identifiers are converted to and are stored as Unicode (UTF-8). The permissible Unicode characters in identifiers are those in the Basic Multilingual Plane (BMP). Supplementary characters are not permitted. Identifiers thus may contain these characters:在内部,标识符被转换为Unicode(UTF-8)并存储为Unicode。标识符中允许的Unicode字符是基本多语言平面(BMP)中的字符。不允许使用补充字符。因此,标识符可能包含以下字符:
Permitted characters in unquoted identifiers:未加引号的标识符中允许的字符:
ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)(基本拉丁字母、数字0-9、美元、下划线)
Extended: U+0080 .. U+FFFF
Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000:带引号的标识符中允许的字符包括完整的Unicode基本多语言平面(BMP),U+0000除外:
ASCII: U+0001 .. U+007F
Extended: U+0080 .. U+FFFF
ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers.ASCII NUL(U+0000)和补充字符(U+10000及以上)不允许出现在带引号或不带引号的标识符中。
Identifiers may begin with a digit but unless quoted may not consist solely of digits.标识符可以以数字开头,但除非引用,否则不能仅由数字组成。
Database, table, and column names cannot end with space characters.数据库、表和列名不能以空格字符结尾。
The identifier quote character is the backtick (标识符引号字符是反斜点(`
):`
):
mysql> SELECT * FROM `select` WHERE `select`.id > 100;
If the 如果启用了ANSI_QUOTES
SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks:ANSI_QUOTES
SQL模式,也允许在双引号内引用标识符:
mysql>CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax... mysql>SET sql_mode='ANSI_QUOTES';
mysql>CREATE TABLE "test" (col INT);
Query OK, 0 rows affected (0.00 sec)
The ANSI_QUOTES
mode causes the server to interpret double-quoted strings as identifiers. ANSI_QUOTES
模式使服务器将双引号字符串解释为标识符。Consequently, when this mode is enabled, string literals must be enclosed within single quotation marks. They cannot be enclosed within double quotation marks. The server SQL mode is controlled as described in Section 5.1.11, “Server SQL Modes”.因此,当启用此模式时,字符串文字必须包含在单引号内。它们不能包含在双引号内。服务器SQL模式按照第5.1.11节,“服务器SQL模式”所述进行控制。
Identifier quote characters can be included within an identifier if you quote the identifier. If the character to be included within the identifier is the same as that used to quote the identifier itself, then you need to double the character. 如果引用标识符,则标识符中可以包含标识符引号字符。如果要包含在标识符中的字符与用于引用标识符本身的字符相同,则需要将该字符加倍。The following statement creates a table named 下面的语句创建一个名为a`b
that contains a column named c"d
:a`b
的表,该表包含一列名为c"d
:
mysql> CREATE TABLE `a``b` (`c"d` INT);
In the select list of a query, a quoted column alias can be specified using identifier or string quoting characters:在查询的选择列表中,可以使用标识符或字符串引用字符指定带引号的列别名:
mysql> SELECT 1 AS `one`, 2 AS 'two';
+-----+-----+
| one | two |
+-----+-----+
| 1 | 2 |
+-----+-----+
Elsewhere in the statement, quoted references to the alias must use identifier quoting or the reference is treated as a string literal.在语句的其他地方,引用别名的引用必须使用标识符引用,否则引用将被视为字符串文字。
It is recommended that you do not use names that begin with 建议不要使用以
or M
e
, where M
eN
M
and N
are integers. Me
或MeN
开头的名称,其中M
和N
是整数。For example, avoid using 例如,避免使用1e
as an identifier, because an expression such as 1e+3
is ambiguous. 1e
作为标识符,因为诸如1e+3
之类的表达式是不明确的。Depending on context, it might be interpreted as the expression 根据上下文,它可能被解释为表达式1e + 3
or as the number 1e+3
.1e + 3
或数字1e+3
。
Be careful when using 使用MD5()
to produce table names because it can produce names in illegal or ambiguous formats such as those just described.MD5()
生成表名时要小心,因为它可能会生成非法或不明确格式的名称,例如刚才描述的格式。
A user variable cannot be used directly in an SQL statement as an identifier or as part of an identifier. 用户变量不能直接在SQL语句中用作标识符或标识符的一部分。See Section 9.4, “User-Defined Variables”, for more information and examples of workarounds.有关解决方法的更多信息和示例,请参阅第9.4节,“用户定义变量”。
Special characters in database and table names are encoded in the corresponding file system names as described in Section 9.2.4, “Mapping of Identifiers to File Names”.数据库和表名称中的特殊字符编码在相应的文件系统名称中,如第9.2.4节,“标识符到文件名的映射”所述。