Previous Section  < Day Day Up >  Next Section

6.7 Comments in SQL Statements

MySQL supports three forms of comment syntax. One of those forms has some variants that allow special instructions to be passed through to the MySQL server.

  • A # character begins a comment that extends to the end of the line. This commenting style is like that used by several other programs, such as Perl, Awk, and several Unix shells.

  • A /* sequence begins a comment that ends with a */ sequence:

    
    
    
    

    
    /* this is a comment */
    
    

    This style is the same as that used for writing comments in the C programming language. A C-style comment may span multiple lines:

    
    
    
    

    
    /*
    
      this
    
      is a
    
      comment
    
    */
    
    

  • A -- (double dash) sequence followed by a space begins a comment that extends to the end of the line. This syntax requires a space and thus differs from standard ANSI SQL syntax, which allows comments to be introduced by -- without the space. MySQL disallows a double dash without a space as a comment because it's ambiguous. (For example, does 1--3 mean "one minus negative three" or "one followed by a comment"?)

C-style comments can contain embedded SQL text that's treated specially by the MySQL server, but ignored by other database engines. This is an aid to writing more portable SQL; it enables you to write comments that are treated as part of the surrounding statement if executed by MySQL and ignored if executed by other database servers. There are two ways to write embedded SQL in a C-style comment:

  • If the comment begins with /*! rather than with /*, MySQL executes the body of the comment as part of the surrounding query. The following statement creates a table named t, but for MySQL creates it as a HEAP table:

    
    
    
    

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

  • If the comment begins with /*! followed by a version number, the embedded SQL is version specific. The server executes the body of the comment as part of the surrounding query if its version is at least as recent as that specified in the query. Otherwise, it ignores the comment. For example, HEAP tables appeared in MySQL 3.23.0. To write a comment that's understood only by servers from MySQL 3.23.0 and up and ignored by older servers, write it as follows:

    
    
    
    

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

    Previous Section  < Day Day Up >  Next Section