MySQL Comments

Learn about the various types of comments in MySQL, and how to use the comments with MySQL queries.
Submitted by Apurva Mathur, on September 20, 2022

Comments in MySQL

We know that comments play an important role when it comes to any programming language, as it tells the other programmer why the section of code is written, and what function it is performing. Also, it is always a good habit when you use comments. Maintaining a code becomes a lot easier when you have comments in your code. Comments never affect the running code as comments are only human-readable concepts and not machine language concepts.

Types of MySQL Comments

There are two types of comments in MySQL,

  1. Single line comment
  2. Multi-line comments

1) MySQL Single Line Comment

There are two ways to write a single-line comment in MySQL. By using the # (hash character) and By using the -- (double dash) characters. In both of these cases, the text written after the hash character (#) or double dash (--) characters to the end of the line will be considered as the comment and will not be executed.

Syntax:

# The comment text will be here 
-- The comment text will be here

Example:

mysql> SELECT 1+2;     # This is a single line comment 
mysql> SELECT 2+3;     -- This is also a single line comment
SELECT * FROM Customers; -- Select the list of all customers
SELECT * FROM Customers; # Select the list of all customers

See the below query and its output, please note comment is written with the query.

As you can see my comment does not affect the query, always write your single-line comment after the query.

Example 1: MySQL Comment

2) MySQL Multi Line Comment

Just like C language, the multi-line comment is used to comment on the text in multiple lines. To enable/write the multi-line comment, we use the sequence of the characters (/* and */). Any text written within these characters will be considered comments. This style written between the beginning and closing sequences enables a comment to extend over multiple lines.

Syntax:

/*
The comment text will be here (line 1)
The comment text will be here (line 2)
...
*/

Example:

mysql> SELECT 1+
/*
An example of multi-line comment
This is line 1
This is line 2
… and so on… 
*/
2; 


SELECT CustomerName, City, Country FROM Customers
/*
This query will display three columns
CustomerName, City and Country
of the Customers table
*/
;

See the below query and its output, please note comment is written with the query.

Example 2: MySQL Comment

All the multiple-line comments are written between the /* */, and they will all be ignored by the machine it will only execute the query.





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.