標籤: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`”才會被執行。
mysql伺服器的注釋總結