Watch Those MySQL Comments

Ugh, I just wasted time debugging a MySQL script only to find that I had created a syntactically invalid comment. So, as a future reminder to myself (and to warn others), MySQL supports 3 different comment styles:
# marks the start of a comment (until the end of the line)
— also marks the start of a comment (until the end of a line)
/* */ can be used to mark the start and end of comments (supporting multi-line comment, too).
# ## and even ######### are all valid comment designators. But you need to be careful with — (double hyphen) because — is only a comment designator if it is followed by a space. Use more than 2 hyphens, or omit the space between the hyphens and the comment text, and all of the text will be part of your SQL.

2 responses to “Watch Those MySQL Comments”

  1. Barney Avatar

    Also, if you use a ’til end of line’ comment (either format) as the last line of a query, you’ll throw an error when the query tries to replicate from master to slave. I’ve fought SQL comments enough times that I always just use CF comments inside the SQL.

  2. Eric Thompson Avatar
    Eric Thompson

    The behavior of the "–" comment drove me nuts until I started using an editor familiar with MySQL’s dialect. I was rather dumbfounded at the persnicketiness of the comment formatting.

Leave a Reply