Сервер MySQL поддерживает следующие способы задания комментариев: с
помощью символа `#', за которым следует текст комментария до конца строки;
с помощью двух символов --
, за которыми идет текст комментария до конца
строки; и (для многострочных комментариев) с помощью символов /*
(начало
комментария) и */
(конец комментария):
mysql> SELECT 1+1; # Этот комментарий продолжается до конца строки mysql> SELECT 1+1; -- Этот комментарий продолжается до конца строки mysql> SELECT 1 /* Это комментарий в строке */ + 1; mysql> SELECT 1+ /* Это многострочный комментарий */ 1;
Обратите внимание: при использовании для комментирования способа с --
(двойное тире) требуется наличие хотя бы одного пробела после второго
тире!
Хотя сервер ``понимает'' все описанные выше варианты комментирования,
существует ряд ограничений на способ синтаксического анализа комментариев
вида /* ... */
клиентом mysql
:
mysql
в
интерактивном режиме эта ошибка проявится в том, что окно запроса
изменит свое состояние с mysql>
на '>
или ">
.
Эти ограничения относятся как к интерактивному режиму работы mysql
(из
командной строки), так и к вызову команд из файла, читаемого с ввода
командой mysql < some-file
.
MySQL поддерживает принятый в ANSI SQL способ комментирования с помощью двойного тире `--' только в том случае, если после второго тире следует пробел (see section 1.9.4.7 Символы `--' как начало комментария).
User Comments
YOu may also embed non-ANSI MYSQL commands
or commands specifiec to a particular version in the
comment syntax using the /*!xxxx */ syntax See
section:
1.7.3 MySQL Extensions to ANSI SQL92
In version 3.23, a semicolon in a multiline
comment will cause an error. Including the GPL
header in a comment, for instance, will generate
lots of errors. (Legalese uses lots of semicolons).
Some tiny gotchas-- nothing serious. Using two dashes ('--') inside a regular comment block (/* ... */) __MAY__ confuse the parser and in which case you will get strange errors. Also using single apostrophe's (') can wig it out too. Not sure why this happens.
/*
this is a good comment
*/
/* this is a bad comment-- it has two dashes
and __MIGHT__ wig out the parser
(for some reason it doesn't always happen, haven't
figure out the how/when part yet)
*/
My guess is the Parser "Wigs Out" when you include the double dash on the same line as the */, so that it looks like this:
/* Start a Comment
-- Here's a nested comment */
Now, the parser saw the /* and the --, but because of the --, it didn't see the */ and so it thinks the original /* comment is still active. It closed the -- comment on the carriage return.
The parser should either NOT recognize nested comments, or be adjusted to allow for closing one comment while "technically" inside another.
note that mysql parser does not always interpret correctly C -style comments
1 row in set (0.00 sec)here's an example:
correct behaviour:
mysql> select 1+ /* aaa */ 2 jj;
"funny" :
mysql> select 1+ /* aaa */ 2;
1 row in set (0.00 sec)
and if you have new lines in the comment (just copy the example from the documentation) it gets too funny
One combination in a source'd file that just caused me grief is # embedded within /* */:
/* Insert #1 of 4 */
insert into foo (bar) values (1);
The insert is skipped (0 rows affected)
This is better:
/* Insert number 1 of 4 */
insert into foo (bar) values (1);
Add your own comment.