Mysql 斜杠星号爆炸

Mysql slash asterisk bang

我刚刚意识到 sql 文件 (.sql) 中有以 /*! 开头的行,它们不是注释,执行它们是为了执行一些系统级任务。

我想知道以 /*! 开头的行对于 MySQL 的真正含义以及为什么在符号旁边使用该数字(在我的示例中为 4000 ).

示例如下:

/*!40000 ALTER TABLE `my_table` DISABLE KEYS */;
INSERT INTO my_table VALUES ('value1','value2');
/*!40000 ALTER TABLE `my_table` ENABLE KEYS */;

编辑: 根据 linked question,我不认为这个问题是重复的。这个问题是关于MySQL如何对待它的。它没有询问如何删除这些评论,问题也没有将其视为 MySQL.

的评论

这些行被 MySQL 以外的任何 RDBMS 视为注释。 MySQL 但是会执行它们。

这会在 SQL 文件中启用 MySQL 特定关键字或语法,而不会破坏其他系统。

数字代表您定位的版本,例如/*!32302 TEMPORARY */ 将仅由 MySQL 版本 3.23.02 或更高版本执行。

查看 the docs 了解更多信息:

MySQL Server supports some 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 */

In this case, MySQL Server parses and executes the code within the comment as it would any other SQL statement, but other SQL servers will ignore the extensions. For example, MySQL Server recognizes the STRAIGHT_JOIN keyword in the following statement, but other servers will not:

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