Some other SQL databases use `--' to start comments.
MySQL Server has `#' as the start comment character. You can also use
the C comment style /* this is a comment */
with MySQL Server.
See section 6.1.6 Comment Syntax.
MySQL Server Version 3.23.3 and above support the `--' comment style,
provided the comment is followed by a space. This is because this
comment style has caused many 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 negative.
Because 1--1
is legal in SQL, the consequences of allowing
comments to start with `--' are terrible.
Using our implementation of this method of commenting in MySQL Server
Version 3.23.3 and up, 1-- This is a comment
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:
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
Posted by reaganpr on Tuesday January 28 2003, @7:57am | [Delete] [Edit] |
I used mysqldump to make a dump of a single database on server version 3.23.52 and was unable to import that data on a 3.23.43 server. Although this manual entry states that server versions > 3.23.3 recognize the new '--', I discovered that this particular server version chokes on lines that consist of just dashes ('-').
I also couldn't get the 'replace' example to work on my system until I did:
shell> replace "\^--" "#" -- my-file.sql
Which will replace only lines that start with '--'. I hope this helps someone with a similar problem.
p.