10.7. Comments in SQL Statements


MySQL supports three forms of comment syntax. One of those forms has 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 style is the same as that used for writing comments in the C programming language. A C-style comment may occur on a single line or span multiple lines:

     /* this is a comment */ /*   this   is a   comment,   too */ 

  • A -- (double dash) sequence followed by a space (or control character) begins a comment that extends to the end of the line. This syntax requires a space and thus differs from standard 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 because 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 specifically as a MEMORY table:

     CREATE TABLE t (i INT) /*! ENGINE = MEMORY */; 

  • 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, the FULL keyword for SHOW TABLES was added in MySQL 5.0.2. To write a comment that's understood only by servers from MySQL 5.0.2 and up and ignored by older servers, write it as follows:

     SHOW /*!50002 FULL */ TABLES; 



MySQL 5 Certification Study Guide
MySQL 5.0 Certification Study Guide
ISBN: 0672328127
EAN: 2147483647
Year: 2006
Pages: 312

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net