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.


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.