MySQL 服务器支持三种注释风格:MySQL Server supports three comment styles:
从一个
#字符到行尾。From a#character to the end of the line.从一个
--序列到行尾。在 MySQL 中,--(双减号)注释样式要求第二个减号后必须跟随至少一个空白字符或控制字符,例如空格或制表符。此语法与标准 SQL 注释语法略有不同,如 第 1.7.2.4 节,“'--' 作为注释的开始” 所讨论的内容。From a--sequence to the end of the line. In MySQL, the--(double-dash) comment style requires the second dash to be followed by at least one whitespace or control character, such as a space or tab. This syntax differs slightly from standard SQL comment syntax, as discussed in Section 1.7.2.4, “'--' as the Start of a Comment”.从一个
/*序列到随后的*/序列,就像在 C 语言中一样。此语法允许注释跨多行,因为开始和结束的序列不必在同一行上。From a/*sequence to the following*/sequence, as in the C programming language. This syntax enables a comment to extend over multiple lines because the beginning and closing sequences need not be on the same line.
下列示例演示了所有三种注释风格:The following example demonstrates all three comment styles:
mysql>SELECT 1+1; # This comment continues to the end of linemysql>SELECT 1+1; -- This comment continues to the end of linemysql>SELECT 1 /* this is an in-line comment */ + 1;mysql>SELECT 1+/*this is amultiple-line comment*/1;
不支持嵌套注释,并且已被弃用;预计将在未来的 MySQL 版本中移除它们。(在某些情况下,嵌套注释可能被允许,但通常不允许,用户应避免使用它们。)Nested comments are not supported, and are deprecated; expect them to be removed in a future MySQL release. (Under some conditions, nested comments might be permitted, but usually are not, and users should avoid them.)
MySQL 服务器支持某些 C 风格注释的变体。这些变体使您能够编写包含 MySQL 扩展的代码,同时仍然具有可移植性,方法是使用以下形式的注释:MySQL Server supports certain variants of C-style comments. These enable you to write code that includes MySQL extensions, but is still portable, by using comments of the following form:
/*! MySQL-specific code */
在这种情况下,MySQL Server 会像解析任何其他 SQL 语句一样解析并执行注释中的代码,但其他 SQL 服务器应忽略扩展。例如,MySQL 服务器可以识别以下语句中的 STRAIGHT_JOIN 关键字,但其他服务器则不能:In this case, MySQL Server parses and executes the code within the
comment as it would any other SQL statement, but other SQL servers
should ignore the extensions. For example, MySQL Server recognizes
the STRAIGHT_JOIN keyword in the following
statement, but other servers should not:
SELECT /*! STRAIGHT_JOIN */ col1 FROM table1,table2 WHERE ...
如果在 ! 字符后添加版本号,则评论中的语法仅在 MySQL 版本大于或等于指定版本号时执行。以下评论中的 KEY_BLOCK_SIZE 关键字仅由 MySQL 5.1.10 或更高版本的服务器执行:If you add a version number after the !
character, the syntax within the comment is executed only if the
MySQL version is greater than or equal to the specified version
number. The KEY_BLOCK_SIZE keyword in the
following comment is executed only by servers from MySQL 5.1.10 or
higher:
CREATE TABLE t1(a INT, KEY (a)) /*!50110 KEY_BLOCK_SIZE=1024 */;
版本号使用 Mmmrr 格式,其中 M 是主版本号,mm 是两位数的次版本号,rr 是两位数的发布号。例如:对于仅由 MySQL 8.4.9 或更高版本的服务器运行的语句,在注释中使用 80409。The version number uses the format
Mmmrr, where
M is a major version,
mm is a two-digit minor version, and
rr is a two-digit release number. For
example: In a statement to be run only by a MySQL server version
8.4.9 or later, use
80409 in the comment.
在 MySQL 8.4 中,版本号也可以选择由六位数字组成,格式为 MMmmrr,其中 MM 是两位数的主版本号,mm 和 rr 分别是两位数的次版本号和两位数的发布号。In MySQL 8.4, the version number can also optionally
be comprised of six digits in MMmmrr
format, where MM is a two-digit major
version, and mm and
rr are the two-digit minor version and
two-digit release numbers, respectively.
版本号后应至少跟随一个空白字符(或注释结尾)。如果注释以六位数字开头,后跟空白字符,则将其解释为六位数版本号。否则,如果以至少五位数字开头,则这些数字被解释为五位数版本号(其余字符在此目的下被忽略);如果以少于五位数字开头,则该注释将作为普通 MySQL 注释处理。The version number should be followed by at least one whitespace character (or the end of the comment). If the comment begins with six digits followed by whitespace, this is interpreted as a six-digit version number. Otherwise, if it begins with at least five digits, these are interpreted as a five-digit version number (and any remaining characters ignored for this purpose); if it begins with fewer than five digits, the comment is handled as a normal MySQL comment.
刚才描述的注释语法适用于 mysqld 服务器解析 SQL 语句的方式。mysql 客户端程序在将语句发送到服务器之前也会对语句进行一些解析。(它这样做是为了确定多语句输入行中的语句边界。)有关服务器解析器与 mysql 客户端解析器之间差异的信息,请参见 第 6.5.1.6 节,“mysql 客户端技巧”。The comment syntax just described applies to how the mysqld server parses SQL statements. The mysql client program also performs some parsing of statements before sending them to the server. (It does this to determine statement boundaries within a multiple-statement input line.) For information about differences between the server and mysql client parsers, see Section 6.5.1.6, “mysql Client Tips”.
以 /*!12345 ... */ 格式的注释不会存储在服务器上。如果使用此格式对存储程序进行注释,这些注释不会保留在程序主体中。Comments in /*!12345 ... */ format are not
stored on the server. If this format is used to comment stored
programs, the comments are not retained in the program body.
C 风格注释语法的另一种变体用于指定优化器提示。提示注释在 /* 注释开头序列后包含一个 + 字符。例如:Another variant of C-style comment syntax is used to specify
optimizer hints. Hint comments include a +
character following the /* comment opening
sequence. Example:
SELECT /*+ BKA(t1) */ FROM ... ;
更多信息,请参见 第 10.9.3 节,“优化器提示”。For more information, see Section 10.9.3, “Optimizer Hints”.
在多行 \C 注释中使用短格式的 mysql 命令(例如 /* ...
*/)是不支持的。短格式命令可以在单行 /*! ... */ 版本注释中使用,/*+ ... */ 优化器提示注释也可以使用,这些注释会被存储在对象定义中。如果担心优化器提示注释可能存储在对象定义中,从而在使用 mysql 重新加载转储文件时会执行此类命令,可以通过使用 mysql 并带上 --binary-mode 选项,或者使用除 mysql 之外的其他重新加载客户端来解决。The use of short-form mysql commands such as
\C within multiple-line /* ...
*/ comments is not supported. Short-form commands do
work within single-line /*! ... */ version
comments, as do /*+ ... */ optimizer-hint
comments, which are stored in object definitions. If there is a
concern that optimizer-hint comments may be stored in object
definitions so that dump files when reloaded with
mysql would result in execution of such
commands, either invoke mysql with the
--binary-mode option or use a reload
client other than mysql.