comment multiple lines in mysql

How to Comment Multiple Lines in MySQL

MySQL allows you to add comments within your SQL queries to make it easy to read for others. These comments are not executed during query parsing and allow you to easily explain query logic or important information. Sometimes you may need to comment single line, sometimes you may need to comment multiple lines in MySQL. In this article, we will look at the different types of comments supported in MySQL.


How to Comment Multiple Lines in MySQL

MySQL supports 4 types of comments.


1. Using double dash

You can use double dash ‘– ‘ to specify start of comment in a MySQL query. Here is an example of comment using double dash indicator.

mysql> select * from data; -- this is a comment

Please note, you need to add a whitespace character such as space, tab, newline after the second dash in double dash. Otherwise, MySQL will misinterpret it as — operator.


2. Using hash operator

Similarly, you can also use hash ‘#’ operator to specify comment. The text from # symbol to end of line is counted as a comment

mysql> select * from data; #this is a comment

Both double dash and hash symbols can be used for only single line comments. To comment multiple lines, you need to use the next operator.


3. C-Style comments

If you want to comment multiple lines, you can use c-style comments /*..*/, that is, you need to enclose your comment within /*..*/. Here is an example.

/* here is a multiline
comment for your reference */
select * from data;


4. Executable Comments

MySQL also supports comments that can be executed only in MySQL, but not in other databases. It is /*! …*/ It is similar to c-style comments, except that you need to add exclamation mark (!) after asterisk.

select 1 /*! + 2 */

In the above query, the part within comment is executed only in MySQL database. If you run the query in MySQL, you will get result as 3 but if you run it in other databases, you will get result as 1. If you want to specify minimum MySQL version to run a specific query, you specify it after the exclamation mark. For example the following comment will be executed only in MySQL version 5.1.12 or above.

select 1 /*!50112 + 2 */

That’s it. In this article, we have learnt the different types of comments supported by MySQL. You can use any of them, depending on your requirement. Of the available comments, the double dash and hash support only single line comments while c-style comments support multiline comments.

Also read:

How to Install Mumble Server in Linux
How to Reboot Linux Server from Putty
How to Disable/Stop Firewalld in CentOS, Redhat
How to Export PostgreSQL Table to CSV
How to Add Minutes to Datetime in Python

Leave a Reply

Your email address will not be published. Required fields are marked *