A comment is text the database ignores. Comments explain what a query is for, and they are also a quick way to switch part of a query off while you work on it. SQL has two kinds.
The examples use the Productions table from the example database.
Single-line comments
Section titled “Single-line comments”Two hyphens start a comment that runs to the end of the line:
-- Productions made in the UKSELECT Title, CountryFROM ProductionsWHERE Country = 'UK';A -- comment can also follow code on the same line, which is handy for explaining a single column or condition:
SELECT Title, -- the production's name CountryFROM Productions;MySQL needs a space after the two hyphens, so write -- note rather than --note.
Block comments
Section titled “Block comments”/* starts a comment and */ ends it. Everything between is ignored, however many lines it spans:
/* Budget review: list every production with its budget, largest first, for the quarterly report.*/SELECT Title, BudgetFROM ProductionsORDER BY Budget DESC;Switching code off
Section titled “Switching code off”Because a block comment can start and end anywhere, it can switch off part of a statement and leave the rest running. Here the Format column and one of the conditions are switched off:
SELECT Title, /* Format, */ CountryFROM ProductionsWHERE Country = 'UK' /* AND Budget > 10000000 */;| Title | Country |
|---|---|
| The Lantern Keepers | UK |
| Quarry Lane | UK |
| Six Bells | UK |