9.7 Comments注释

MySQL Server supports three comment styles:MySQL Server支持三种注释样式:

The following example demonstrates all three comment styles:以下示例演示了所有三种注释样式:

mysql> SELECT 1+1;     # This comment continues to the end of line
mysql> SELECT 1+1;     -- This comment continues to the end of line
mysql> SELECT 1 /* this is an in-line comment */ + 1;
mysql> SELECT 1+
/*
this is a
multiple-line comment
*/
1;

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版本中被删除。(在某些情况下,嵌套注释可能是允许的,但通常是不允许的,用户应该避免使用。)

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 Server支持C风格注释的某些变体。通过使用以下形式的注释,您可以编写包含MySQL扩展但仍然可移植的代码:

/*! MySQL-specific code */

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:在这种情况下,MySQL Server会像解析任何其他SQL语句一样解析并执行注释中的代码,但其他SQL服务器应该忽略扩展。例如,MySQL Server可以识别以下语句中的STRIGHT_JOIN关键字,但其他服务器不应该识别:

SELECT /*! STRAIGHT_JOIN */ col1 FROM table1,table2 WHERE ...

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. 如果在之后添加版本号!字符,只有当MySQL版本大于或等于指定的版本号时,才会执行注释中的语法。The KEY_BLOCK_SIZE keyword in the following comment is executed only by servers from MySQL 5.1.10 or higher:以下注释中的KEY_BLOCK_SIZE关键字仅由MySQL 5.1.10或更高版本的服务器执行:

CREATE TABLE t1(a INT, KEY (a)) /*!50110 KEY_BLOCK_SIZE=1024 */;

The comment syntax just described applies to how the mysqld server parses SQL statements. 刚才描述的注释语法适用于mysqld服务器解析SQL语句的方式。The mysql client program also performs some parsing of statements before sending them to the server. 在将语句发送到服务器之前,mysql客户端程序还会对语句进行一些解析。(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 4.5.1.6, “mysql Client Tips”.(这样做是为了确定多语句输入行中的语句边界。)有关服务器和mysql客户端解析器之间差异的信息,请参阅第4.5.1.6节,“mysql客户端提示”

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./*!12345 ... */格式的注释未存储在服务器上。如果此格式用于对存储的程序进行注释,则注释不会保留在程序体中。

Another variant of C-style comment syntax is used to specify optimizer hints. Hint comments include a + character following the /* comment opening sequence. Example:C风格注释语法的另一个变体用于指定优化器提示。提示注释包括/*注释开头序列后面的+字符。例如:

SELECT /*+ BKA(t1) */ FROM ... ;

For more information, see Section 8.9.3, “Optimizer Hints”.有关更多信息,请参阅第8.9.3节,“优化器提示”

The use of short-form mysql commands such as \C within multiple-line /* ... */ comments is not supported. 不支持在多行/* ... */注释中使用短格式的mysql命令,如\CShort-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.如果担心优化器提示注释可能存储在对象定义中,从而在使用mysql重新加载转储文件时会导致执行此类命令,请使用--binary-mode选项调用mysql,或者使用mysql以外的重新加载客户端。