This post will discuss single and multiple-line comments in MySQL with examples.
MySQL Single/End Line Comment Function
In MySQL, the single-line comments are placed at the end of the line to ignore that part of the line. There are two styles to write a single-line comment:
- To use the “#” symbol and place comment after it
- To use the “—” symbols with a single space, after that, place the comment
Note: Any command after these symbols will be considered as a comment and will be ignored.
Example 1: The “#” Symbol Single/End Line Comment
If the user adds a comment at the end of the line, it will be ignored, and the query that is placed before the comment will execute. Run this query with a comment to see the output:
The output displays the records based on “SELECT * FROM Customer WHERE id <= 10;” and ignoring the comment successfully:
Example 2: The “–” Symbol Single/End Line Comment
In MySQL the single line comment that starts with “–” symbol is ignored till the end of the line. Let’s add a comment at the end of the query and see if it is ignored or not:
The query executed and ignored the comment successfully:
MySQL Multi-Line Comment Function
In MySQL multi-line comments start with “/*” and end with “*/” symbols. MySQL ignores anything that is placed inside these symbols. Let’s see an example and place this comment at the end of a query:
The output displays all the records obtained by the query placed behind the multi-line comment:
Place the comment inside the query, to see if the query runs by ignoring the comment:
The output is showing the result of the query “SELECT * FROM Customer WHERE id <= 10;” and ignores the comment:
The comment can exceed multiple lines, to explain the purpose of the query and will not affect its operation. Let’s see an example by running this given below query:
/* multi line
comment */
WHERE id <= 10;
The output displays the record obtained by running the query “SELECT * FROM Customer WHERE id <= 10;” and ignored the comment successfully:
You have learned about MySQL comment function.
Conclusion
The comments in MySQL explain MySQL queries and statements without affecting their workings. The single/end-line comments ignore anything that comes after the symbol “#” or “—” symbols till the end of the line. Whereas multi-line comment ignores anything that comes in between “/*” and “*/” symbols. This post discusses MySQL Comment Function in detail along with demonstrations.