mysql注释用处_mysql服务器的注释总结

MySQL允许在SQL 代码中使用注释。这对于阅读代码很有用处。MySQL服务器支持3种注释风格:

1、“#”注释

以“#”字符开始,到所在行结尾的所有字符。

mysql> select @schema; #which schema

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.00 sec)

mysql> select @schema; # which schema

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.00 sec)

字符“#”和之后的字符之间,有无空格均可。

2、“--”+“空格”注释

用两个短划线和一个空格注释;从这两个短划线到行的结束的所有内容都作为注释处理。以“--”字符开始,到所在行结尾的所有字符。特别注意:双破折号要求第2个破折号后面至少跟一个空格符(例如空格、tab、换行符等等)。

2.1.不带空格的情况:

mysql> select @schema; --which schema

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.00 sec)

-> ;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '--which schema' at line 1

mysql>

2.2.带空格的情况:

mysql> select @schema; -- which schema

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.00 sec)

3、“/*  */”单行或者多行注释

从“/*”字符开始,到“*/”结尾。结束序列不一定在同一行中,因此该语法允许注释跨越多行。

3.1.单行注释

3.1.1.和注释内容之间无空格

mysql> select @schema; /*which schema*/

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.00 sec)

3.1.2.“/*”和注释内容之间有空格

mysql> select @schema; / *which schema*/

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.01 sec)

-> ;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/ *which schema*/' at line 1

3.1.3.“*/”和注释内容之间有空格

mysql> select @schema; /*which schema */

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.00 sec)

3.2.多行注释

3.2.1.和注释内容之间无空格

诸如如下例子:

select @schema

/* which schema

how to get schema*/;

mysql> select @schema

-> /*which schema

/*> how to get schema*/;

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.00 sec)

3.2.2.“*/”和注释内容之间有空格

mysql> select @schema

-> /*which schema

/*> how to get schema */;

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.00 sec)

3.2.2.“/*”和注释内容之间有空格

mysql> select @schema

-> /* which schema

/*> how to get schema*/;

+---------+

| @schema |

+---------+

| NULL    |

+---------+

1 row in set (0.00 sec)

综述,“/* */”单行注释内容时,“/*”和注释内容之间不能有空格;

“/* */”多行注释内容时,“/* */”和注释内容之间有无空格均可。

4、“/*! */”注释

隐藏MySQL特有的关键字,注释以“/ * !”而不是以“ / *”起头。MySQL查看这种特殊类型注释的内部并使用这些关键字,但其他数据库服务器将这些关键字作为注释的一部分忽略。这样有助于编写由MySQL执行时利用MySQL特有功能的代码,而且该代码也可以不用修改就用于其他数据库服务器。

例如:

/*!50001 DROP TABLE IF EXISTS `count_yysbh`*/;

50001表示假如数据库版本是5.00.01以上版本,“DROP TABLE IF EXISTS `count_yysbh`”才会被执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值