Team LiB
Previous Section Next Section

Comment Syntax

MySQL allows you to intersperse comments with your SQL code. Comments can be useful for documenting statements that you store in files. This section describes how to write comments in your SQL statements.

The MySQL server understands three types of comments:

  • Anything from '#' to the end of the line is treated as a comment. This syntax is the same as is used in most Unix shells and Perl.

    # this is a single line comment
    

  • Anything between '/*' and '*/' is treated as a comment. This form of comment may span multiple lines. The syntax is the same as is used in the C programming language.

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

  • You can begin a comment with two dashes and a space ('-- '), or two dashes and a control character such as a newline. Everything from the dashes to the end of the line is treated as a comment.

    --
    -- This is a comment
    --
    

    This is a bit different from the comment style of standard SQL, which begins with just two dashes and does not require the space before any following text. MySQL requires a space after the dashes as an aid for disambiguation. Statements with expressions such as 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 intend to port to other databases that don't understand the other comment styles.

Comments are ignored by the server when executing statements, with the exception that C-style comments that begin with '/*!' are given special treatment. 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: It is possible to 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 mytbl (id,date) VALUES(13,'2004-09-28');
INSERT /*! DELAYED */ INTO mytbl (id,date) VALUES(13,'2004-09-28');

C-style comments can be made version-specific. Follow the opening '/*!' sequence with a five-digit 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 4.1.0 or later:

CREATE TABLE t (i INT, INDEX /*!40100 USING BTREE */ (i)) ENGINE = MEMORY;

    Team LiB
    Previous Section Next Section