9.2 Schema Object Names架构对象名称

9.2.1 Identifier Length Limits标识符长度限制
9.2.2 Identifier Qualifiers标识符限定符
9.2.3 Identifier Case Sensitivity标识符区分大小写
9.2.4 Mapping of Identifiers to File Names标识符到文件名的映射
9.2.5 Function Name Parsing and Resolution函数名称解析与解析

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)中的字符。不允许使用补充字符。因此,标识符可能包含以下字符:

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 Me or MeN, where M and N are integers. 建议不要使用以MeMeN开头的名称,其中MN是整数。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节,“标识符到文件名的映射”所述。

9.2.1 Identifier Length Limits
9.2.2 Identifier Qualifiers
9.2.3 Identifier Case Sensitivity
9.2.4 Mapping of Identifiers to File Names
9.2.5 Function Name Parsing and Resolution