MySQL 中的某些对象,包括数据库、表、索引、列、别名、视图、存储过程、分区、表空间、资源组以及其他对象名称,被称为标识符。本节描述了 MySQL 中标识符允许的语法。第 11.2.1 节,“标识符长度限制”,指出了每种类型标识符的最大长度。第 11.2.3 节,“标识符大小写敏感性”,描述了哪些类型的标识符是大小写敏感的以及在什么情况下。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. This section describes the permissible syntax for identifiers in MySQL. Section 11.2.1, “Identifier Length Limits”, indicates the maximum length of each type of identifier. Section 11.2.3, “Identifier Case Sensitivity”, describes which types of identifiers are case-sensitive and under what conditions.
标识符可以加引号或不加引号。如果标识符包含特殊字符或是保留字,则每次引用它时都必须加引号。(例外:限定名中紧跟在点号后的保留字必须作为标识符,因此无需加引号。)保留字列在第 11.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 11.3, “Keywords and Reserved Words”.
在内部,标识符会被转换为并以 Unicode (UTF-8) 存储。标识符中允许使用的 Unicode 字符是基本多文种平面(BMP)中的字符。不允许使用补充字符。因此,标识符可以包含这些字符: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:
未引用标识符允许使用的字符:Permitted characters in unquoted identifiers:
ASCII: [0-9,a-z,A-Z$_](基本拉丁字母、数字 0-9、美元符号、下划线)ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)
扩展: U+0080 .. U+FFFFExtended: U+0080 .. U+FFFF
引用标识符允许使用的字符包括整个 Unicode 基本多文种平面(BMP),但 U+0000 除外:Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000:
ASCII: U+0001 .. U+007F
扩展: U+0080 .. U+FFFFExtended: U+0080 .. U+FFFF
ASCII 空字符(U+0000)和补充字符(U+10000 及更高)在引用或未引用标识符中均不允许使用。ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers.
标识符可以以数字开头,但未加引号时不得仅由数字组成。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.
在数据库、表、视图、列、存储程序或别名的未加引号的名称中使用美元符号作为第一个字符已不推荐,包括与限定符一起使用的此类名称(参见 第 11.2.2 节,“标识符限定符”)。以美元符号开头的未加引号标识符不能包含任何额外的美元符号字符。否则,允许使用前导的美元符号,但会触发弃用警告。Use of the dollar sign as the first character in the unquoted name of a database, table, view, column, stored program, or alias is deprecated, including such names used with qualifiers (see Section 11.2.2, “Identifier Qualifiers”). An unquoted identifier beginning with a dollar sign cannot contain any additional dollar sign characters. Otherwise, the leading dollar sign is permitted but triggers a deprecation warning.
当按照本节后面给出的规则引用时,美元符号仍然可以作为此类标识符的首字符而不会产生警告。The dollar sign can still be used as the leading character of such an identifier without producing the warning, when it is quoted according to the rules given later in this section.
标识符引用字符是反引号(`):The identifier quote character is the backtick
(`):
mysql> SELECT * FROM `select` WHERE `select`.id > 100;
如果启用了 ANSI_QUOTES SQL 模式,也可以使用双引号引用标识符:If the ANSI_QUOTES SQL mode is
enabled, it is also permissible to quote identifiers within double
quotation marks:
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)
ANSI_QUOTES 模式会导致服务器将双引号字符串解释为标识符。因此,当启用此模式时,字符串字面值必须用单引号括起来,不能用双引号括起来。服务器 SQL 模式的控制方式如 第 7.1.11 节,“服务器 SQL 模式” 所述。The ANSI_QUOTES mode causes the
server to interpret double-quoted strings as identifiers.
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 7.1.11, “Server SQL Modes”.
如果将标识符用引号引用,标识符中的引号字符可以包含在标识符内。如果要包含在标识符内的字符与用于引用标识符的字符相同,则需要将该字符加倍。以下语句创建了一个名为 a`b 的表,该表包含一个名为 c"d 的列: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:
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.
建议您不要使用以 或 Me 开头的名称,其中 MeNM 和 N 是整数。例如,避免使用 1e 作为标识符,因为诸如 1e+3 之类的表达式可能会造成歧义。根据上下文,它可能被解释为表达式 1e
+ 3 或数字 1e+3。It is recommended that you do not use names that begin with
or
Me,
where MeNM and
N are integers. For example, avoid
using 1e as an identifier, because an
expression such as 1e+3 is ambiguous. Depending
on context, it might be interpreted as the expression 1e
+ 3 or as the number 1e+3.
在使用 MD5() 生成表名时要小心,因为它可能生成非法或模棱两可的名称,如前面所述的情况。Be careful when using MD5() to
produce table names because it can produce names in illegal or
ambiguous formats such as those just described.
还建议您不要使用以 !hidden! 开头的列名,以确保新名称不会与现有隐藏列用于功能索引的名称冲突。It is also recommended that you do not use column names that begin
with !hidden! to ensure that new names do not
collide with names used by existing hidden columns for functional
indexes.
用户变量不能直接在 SQL 语句中用作标识符或标识符的一部分。有关更多信息和解决方法示例,请参见 第 11.4 节,“用户定义变量”。A user variable cannot be used directly in an SQL statement as an identifier or as part of an identifier. See Section 11.4, “User-Defined Variables”, for more information and examples of workarounds.
数据库和表名中的特殊字符在对应的文件系统名称中进行了编码,如 第 11.2.4 节,“标识符映射到文件名” 所述。Special characters in database and table names are encoded in the corresponding file system names as described in Section 11.2.4, “Mapping of Identifiers to File Names”.