Writing Comments

MySQL allows you to intersperse comments with your SQL code. This can be useful for documenting queries that you store in files. There are two recommended comment styles. First, anything from a '#' character to the end of a line is considered a comment. Second, C-style comments are allowed as well. That is, anything between the '/*' and '*/' beginning and ending markers is considered a comment. C-style comments can span multiple lines:

# this is a single line comment 
/* this is also a single line comment */
/* this, however,
   is a multiple line

A third comment style is available as of MySQL 3.23.3: Begin the comment with two dashes and a space ('-- '); everything from the dashes to the end of the line is treated as a comment. Some other databases use the double dash to begin a comment. MySQL allows this but requires the space as an aid for disambiguation. Statements with expressions like 5--7 might be taken as containing a comment starting sequence otherwise. It's not likely you'd write such an expression as 5--7, so this is a useful heuristic. Still, it is only a heuristic, and it's probably better to use the '#' or '/*...*/' comment styles and resort to double dashes only when writing code that you may port to other databases that don't understand the other comment styles.

As of MySQL 3.22.7, you can "hide" MySQL-specific keywords in C-style comments by beginning the comment with '/*!' rather than with '/*'. MySQL looks inside this special type of comment and uses the keywords, but other database servers will ignore them as part of the comment. This has a portability benefit, at least for other servers that understand C-style comments. You can write code that takes advantage of MySQL-specific functions when executed by MySQL but that can be used with other database servers without modification. The following two statements are equivalent for database servers other than MySQL, but MySQL will perform an INSERT DELAYED operation for the second:

INSERT INTO absence (student_id,date) VALUES(13,'2002-09-28'); 
INSERT /*! DELAYED */ INTO absence (student_id,date) VALUES(13,'2002-09-28');

As of MySQL 3.22.26, C-style comments can be made version-specific. Follow the opening '/*!' sequence with a version number and the server will ignore the comment unless it is at least as recent as the version named. The comment in the following CREATE TABLE statement is ignored unless the server is version 3.23.0 or later:

CREATE TABLE t (i INT) /*!32300 TYPE = HEAP */;