关于MySQL中的/*!…… */意义

飞扬 2010-12-05 08:51:51
在MySQL的SQL语句或SQL文件经常看到如下用户:
SELECT /*!40001 SQL_CACHE */ * FROM pre_common_syscache WHERE cname IN ('ipbanned')

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

请问其中/*!代码 ……*/代表什么意思?可以用于哪些地方?我在MySQL手册中没有看到具体讲解。
谢谢


...全文
6170 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2010-12-05
  • 打赏
  • 举报
回复


[Quote=MySQL 5.4 Reference Manual]8.5. Comment Syntax
MySQL Server supports three comment styles:

From a “#” character to the end of the line.

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, tab, newline, and so on). This syntax differs slightly from standard SQL comment syntax, as discussed in Section 1.7.5.6, “'--' as the Start of a Comment”.

From a /* sequence to the following */ sequence, as in the C programming language. This syntax allows 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 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.

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 ...

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 TEMPORARY keyword in the following comment is executed only by servers from MySQL 3.23.02 or higher:

CREATE /*!32302 TEMPORARY */ TABLE t (a INT);

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.)

The use of short-form mysql commands such as \C within multi-line /* ... */ comments is not supported
[/Quote]


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 */

ACMAIN_CHM 2010-12-05
  • 打赏
  • 举报
回复
[Quote]我在MySQL手册中没有看到具体讲解。[/Quote]这说明你的阅读能力,或者观察能力,再或者搜索能力不有待提高。

[Quote=MySQL 5.1参考手册]1.8.4. MySQL对标准SQL的扩展
MySQL服务器包含一些其他SQL DBMS中不具备的扩展。注意,如果使用了它们,将无法把代码移植到其他SQL服务器。在某些情况下,你可以编写包含MySQL扩展的代码,但仍保持其可移植性,方法是用“/*... */”注释掉这些扩展。在本例中,MySQL服务器能够解析并执行注释中的代码,就像对待其他MySQL语句一样,但其他SQL服务器将忽略这些扩展。例如:

SELECT /*! STRAIGHT_JOIN */ col_name FROM table1,table2 WHERE ...
如果在字符“!”后添加了版本号,仅当MySQL的版本等于或高于指定的版本号时才会执行注释中的语法:

CREATE /*!32302 TEMPORARY */ TABLE t (a INT);
这意味着,如果你的版本号为3.23.02或更高,MySQL服务器将使用TEMPORARY关键字。

[/Quote]
ACMAIN_CHM 2010-12-05
  • 打赏
  • 举报
回复
/* .... */ 在大部分语言中都一样是注释。这个之中的语句是不被执行的。

但MYSQL中 为了保持兼容,比如从mysqldump 导出的SQL语句能被其它数据库直接使用,它把一些特有的仅在MYSQL上的语句放在 /*! ... */ 中,这样这些语句如果在其它数据库中是不会被执行,但在MYSQL中它会执行。
rucypli 2010-12-05
  • 打赏
  • 举报
回复
你可以用下面语句做下试验
show status like 'qcache_%'
select /*!40001 SQL_CACHE */ * FROM tb
select /*!40001 SQL_no_CACHE */ * FROM tb

效果和不加/*!..*/一样
select SQL_CACHE * FROM tb
select SQL_no_CACHE * FROM tb


还有相关变量
变量:query_cache _type,查询缓存的操作模式。有3中模式,0:不缓存;1:缓存查询,除非与 select sql_no_cache开头;2:根据需要只缓存那些以select sql_cache开头的查询; query_cache_size:设置查询缓存的最大结果集的大小,比这个值大的不会被缓存。
iihero_ 2010-12-05
  • 打赏
  • 举报
回复
这只是一个hint啊。
40001 SQL_CACHE, 意味着数据库要缓存整个SQL语句。还有SQL_NO_CACHE的hint
fxs_2008 2010-12-05
  • 打赏
  • 举报
回复
手册上:


但MYSQL中 为了保持兼容标准SQL,

MYSQL上的语句放在 /*! ... */ 中,这样这些语句如果在其它数据库中是不会被执行,但在MYSQL中它会执行

在其他数据库中 /*! ... */就是注释

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧