MySQL Comments – How To Add Comments To Your Queries

Mysql Comments

Comments are a very important part of any programming language. MySQL also provides us with the feature of comments. Like any other programming language, there are multiple types of comments available in MySQL.

In this tutorial, we will learn what are the different types of comments in MySQL and how to use them with practical examples.

Introduction to MySQL Comments

In any programming language, comments help to increase the code readability and understand the code better. It helps not only other programmers but also the owners the next time when they check code.

Comments are not executed by the programming language and don’t derive any output or help any expression. The only function of comments is to understand and explain the code.

Let’s now see what are the types of comments in MySQL.

Types of Comments

There are two types of comments available in MySQL. The first is the single-line comments and the second is multi-line comments. However, MySQL offers three styles of comments to the user out of which two are used to specify a single-line comment and another is used for a multi-line comment.

Below comment styles are available MySQL-

  • # : This specifies a single line comment from the # character to the end of the line.
  • - - : (double dash without space)This also specifies a single line comment from the — sequence to the end of the line. The condition to use the — as a comment style is – the second dash must be followed by at least one whitespace or control character such as newline, space, tab etc.
  • /* */ : If you know the C programming language then you are familiar with this style. This style allows you to write a multi-line comment which is exactly the same as the multi-line comment in the C programming language

Syntax of MySQL Comments

Following are three syntaxes to use comments in MySQL.

Syntax 1. Comment Using #

SELECT * FROM table; #this is a select statement commentCode language: SQL (Structured Query Language) (sql)

Here, everything after the # sign is interpreted as a comment. Note that, this style is only used at the end of the line and must have a line break after the comment ends. You can not insert a comment in-between the SQL statement.

Syntax 2. Comment Using —

SELECT * FROM table; -- this is a select statement commentCode language: SQL (Structured Query Language) (sql)

This comment style is similar to the previous style. It is used for a single line comment and must be used at the end of the line. After the comment ends, no SQL statement should occur in the same line.

Syntax 3. Comment Using /* */

This style is used for multi-line comments in MySQL. You can specify a comment using this style even in-between any SQL statement. Following are the valid syntaxes for the comments created using /* */.

SELECT * FROM table; /* single line comment */Code language: SQL (Structured Query Language) (sql)

Here we have specified a simple single-line comment.

SELECT * FROM table; /* 
multi 
line 
comment 
*/Code language: SQL (Structured Query Language) (sql)

The above query will have three lines of comment. As soon as the end part of the comment (*/) is specified, you can continue to write a query in the same line as well.

SELECT /*this is a valid comment */ col_names FROM TABLE;Code language: SQL (Structured Query Language) (sql)

As stated earlier, we can have a comment between any SQL statement as shown in the above example. This type of comment is created using only the /* */ sequence.

Examples of MySQL Comments

Let’s now look at some of the examples of using MySQL Comments in your code.

Example 1. Single-Line Comment using #

SELECT * FROM accounts; #fetch accounts table dataCode language: SQL (Structured Query Language) (sql)
Single Line Comment Using
Single Line Comment Using

Example 2. Single-Line Comment using —

SELECT * FROM accounts; -- fetch accounts table dataCode language: SQL (Structured Query Language) (sql)
Single Line Comment Using 1 1
Single Line Comment Using 1 1

Example 3. Multi-Line Comment using /* */

Here, we will see two examples. One is a multi-line comment and another one is a comment in-between the SQL statement.

INSERT INTO accounts(name,balance) /*
specified the column names
and insert data into it
*/
VALUES("Rock Hardy",500);Code language: SQL (Structured Query Language) (sql)

Here, we have used the INSERT statement and created a multi-line comment. After ending the comment, we continued to write a SQL statement. Check the below output.

Multi Line Comment Example1
Multi Line Comment Example1
SELECT /*choose col names*/ name,balance FROM accounts;Code language: SQL (Structured Query Language) (sql)

Here, we have inserted a comment between the SQL statement which is a valid comment style. Check the below output.

Multi Line Comment Example2
Multi Line Comment Example2

Summary

In this tutorial, we learned-

  • What is a comment in MySQL?
  • What are the different types of comments in MySQL?
  • What are the available styles of comments in MySQL?
  • Syntax of each comment style.
  • Example of each comment style.

I hope you find this article useful and you liked it. Don’t forget to share it with your friends so that all can learn the topic in an easy way.

References

Official documentation of MySQL comments.