MySQL MariaDB

MySQL Comment Function

Comments in any language are used to explain the code and do not affect the actual work. Similarly, In MySQL, the comment function aids the user in explaining MySQL queries or commenting on the part of the query that should not be executed.

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:

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:

SELECT * FROM Customer WHERE id <= 10; # Add Comment here

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:

SELECT * FROM Customer WHERE id <= 10; -- Add Comment here

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:

SELECT * FROM Customer WHERE id <= 10; /* end line comment */

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:

SELECT * FROM /* in line comment */ Customer WHERE id <= 10;

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:

SELECT * FROM Customer

/* 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.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.