MySQL Reference Manual for version 4.0.18.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.8.5.7 `--' as the Start of a Comment

Some other SQL databases use `--' to start comments. MySQL Server uses `#' as the start comment character. You can also use the C comment style /* this is a comment */ with MySQL Server. See section 10.5 Comment Syntax.

MySQL Server Version 3.23.3 and above support the `--' comment style, provided the comment is followed by a space (or by a control character such as a newline). The requirement for a space is to prevent problems with automatically generated SQL queries that have used something like the following code, where we automatically insert the value of the payment for !payment!:

 
UPDATE tbl_name SET credit=credit-!payment!

Think about what happens if the value of payment is a negative value such as -1:

 
UPDATE tbl_name SET credit=credit--1

credit--1 is a legal expression in SQL, but if -- is interpreted as the start of a comment, part of the expression is discarded. The result is a statement that has a completely different meaning than intended:

 
UPDATE tbl_name SET credit=credit

The statement produces no change in value at all! This illustrates that allowing comments to start with `--' can have serious consequences.

Using our implementation of this method of commenting in MySQL Server Version 3.23.3 and up, credit--1 is actually safe.

Another safe feature is that the mysql command-line client removes all lines that start with `--'.

The following information is relevant only if you are running a MySQL version earlier than 3.23.3:

If you have an SQL program in a text file that contains `--' comments, you should use the replace utility as follows to convert the comments to use `#' characters:

 
shell> replace " --" " #" < text-file-with-funny-comments.sql \
         | mysql database

instead of the usual:

 
shell> mysql database < text-file-with-funny-comments.sql

You can also edit the command file "in place" to change the `--' comments to `#' comments:

 
shell> replace " --" " #" -- text-file-with-funny-comments.sql

Change them back with this command:

 
shell> replace " #" " --" -- text-file-with-funny-comments.sql


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by rdg (Feb 25 2004) using texi2html