MySQL comments are used to add notes, descriptions,s or explanations to the SQL queries. Comments can be used to explain an SQL where clause or table name or the SQL query completely. MySQL supports 3 different comment styles. These are hash (#) sign, doublet dash (–) and /* – */ characters. They provide different use cases and comment creation.
Singleline Comment with Hash Sign (#)
The hash mark can be used to create a singleline comment. When the hash sign is located at the start of the line the whole line became a comment. As a comment non of the characters in this line is executed as SQL.
# This is a comment Select * From Users # This is a singleline comment with hash sign
Singleline Comment with Double Dash (–)
double dash (–) is used to create single line comment too. The double dash is located at the start of the line and the whole line became a comment. The difference between hash sign and double dash comment types is the double dash requires a single space or similar space, tab after the double dash.
-- This is a comment Select * From Users -- This is a singleline comment with hash sign
The following comment is not valid and creates SQL error. As there is no space, tab etc. after the double dash.
--This is not valid
Inline comments are very useful to explain and describe an SQL directly at the same line. The hash mark or double dash can be used to create inline comment. The line starts with a regular SQL query but the comment starts with the hash mark or double dash and comments goes to the end of the line.
Select * From Users # Select all users and related information from User table Select * From Customers -- Select all customers from Customers table
Multiline comments are used to describe in detail and contain multiple lines which are not executed as SQL query. Multiline comments eliminate the line-by-line comment creation and deletion. The /* is used to set start and */ characters are used to set the end of the multiline comment. Every character between /* and */ are interpreted as comments.
Select * From Users /* This is a multiline comment This is multiline and block comment */ Select * From Customers -- Select all customers from Customers table