Search the MySQL manual:
Subscribe to the monthly
MySQL Newsletter!

6.1.6 Синтаксис комментариев

Сервер MySQL поддерживает следующие способы задания комментариев: с помощью символа `#', за которым следует текст комментария до конца строки; с помощью двух символов --, за которыми идет текст комментария до конца строки; и (для многострочных комментариев) с помощью символов /* (начало комментария) и */ (конец комментария):

mysql> SELECT 1+1;     # Этот комментарий продолжается до конца строки
mysql> SELECT 1+1;     -- Этот комментарий продолжается до конца строки
mysql> SELECT 1 /* Это комментарий в строке */ + 1;
mysql> SELECT 1+
/*
Это многострочный
комментарий
*/
1;

Обратите внимание: при использовании для комментирования способа с -- (двойное тире) требуется наличие хотя бы одного пробела после второго тире!

Хотя сервер ``понимает'' все описанные выше варианты комментирования, существует ряд ограничений на способ синтаксического анализа комментариев вида /* ... */ клиентом mysql:

Эти ограничения относятся как к интерактивному режиму работы mysql (из командной строки), так и к вызову команд из файла, читаемого с ввода командой mysql < some-file.

MySQL поддерживает принятый в ANSI SQL способ комментирования с помощью двойного тире `--' только в том случае, если после второго тире следует пробел (see section 1.9.4.7 Символы `--' как начало комментария).

User Comments

Posted by Ken Menzel on August 15 2002 6:21am[Delete] [Edit]

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

Posted by R. Tango-Lowy on September 30 2002 1:40pm[Delete] [Edit]

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

Posted by Brat Wizard on May 27 2003 4:05am[Delete] [Edit]

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

Posted by Stan Bartsch on June 4 2003 9:52pm[Delete] [Edit]

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.

Posted by Philip on January 5 2004 5:39am[Delete] [Edit]

note that mysql parser does not always interpret correctly C -style comments
here's an example:

correct behaviour:
mysql> select 1+ /* aaa */ 2 jj;

+----+
| jj |
+----+
| 3 |
+----+
1 row in set (0.00 sec)


"funny" :
mysql> select 1+ /* aaa */ 2;
+----------------+
| 1+ /* aaa */ 2 |
+----------------+
| 3 |
+----------------+
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

Posted by [name withheld] on March 8 2004 12:39pm[Delete] [Edit]

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.