Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
MySQL Comment Styles
2.1.
Using # Symbol
2.2.
Using - - Symbol
2.3.
Using /* */ Symbol
3.
Executable Comments
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

MySQL Comments

Author Apoorv Dixit
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

A comment is a remark from the programmer to explain things to other programmers or sometimes himself. Comments can be used to document the SQL statements to make them easier to understand. While parsing the SQL code, MySQL ignores the commented part and only executes the non-commented part. 

Like in any other programming language comments are employed to enhance the readability of the code, same as MySQL provides comments to understand the written code in a better way. 

So, Let’s get started now: 

                                      Source: Pinterest

MySQL Comment Styles

MySQL server mainly supports three comment styles.

  1. Using # symbol
  2. Using - symbol 
  3. Using /*  */ symbol

These can be used accordingly to make single or multi-line comments.

Using # Symbol

It is generally placed at the end of the line, and anything written after '# ' till end of the line falls in the scope of the comment. This comment style is generally used to put single-line comments.

Syntax:

SELECT statement; # Comment goes Here

Example

SELECT statement; # WHERE CMARKS >= 80

 

In the above example, the scope of the comment is from ‘#’ to the end of the line. And here, ‘WHERE CMARKS >= 80’ is not a part of the query and will be treated as a comment.

Using - - Symbol

The symbol - -  is placed at the end of the line. And one must ensure that double -hyphen has at least one whitespace character or control character such as newline, tab or space, etc. However, there is no need for whitespace in standard SQL. MySQL uses whitespace to avoid the problems with some SQL statements, such as: 

SELECT 100- - 5; 

The statement will return 105. If MySQL didn’t use the whitespace, it would return 100 instead.

Syntax

SELECT statement; -- comment goes Here

Example

SELECT * FROM Student; -- WHERE CMARKS>=80

In the above example, the query is limited to the ‘;’ semicolon. Anything written after - - is only part of the text to explain things and will not be executed. This comment style is generally used to put single-line comments.

Using /* */ Symbol

This comment style can document a block of SQL code. Comments can span multiple lines. Moreover,  anything written between /* and */ falls under the scope of comment.

Syntax:

/*   
   comment goes here   
   comment goes here  
*/   

Example

/*   
  This is the scope
  Of multi-line 
   Comment.
*/
  
SELECT * FROM Student;

In the above example, the top three lines before the SQL command will be ignored during the execution process. Multiline comments are used for the large text descriptions of code or sometimes to comment out chunks of code while debugging.

Note: It is not always that comments must be multi-line or appear at the end. They can be a single-line and can come between SQL statements if needed. For example:

SELECT * /*comment*/ FROM Student;

 here /*comment*/ is not a part of query.

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Executable Comments

MySQL provides executable comments to allow portability between different databases. This comment styling allows us to embed the SQL code that will only execute in MySQL, and other databases will ignore it.

The syntax of executable comments is: 

/*! MySQL-specific code */

Example:

SELECT 5 /*! +2 */ AS SUM;

 

We can also use the syntax given below to execute the comment in a particular version of MySQL.

/*! #####MySQL-specific code */ 

where, '#####' represents the MySQL version. First, # is used for the major version, the second two hash for the minor, and the last two hash for the patch level.

For example:

CREATE TABLE student (
    k INT AUTO_INCREMENT,
    KEY (k)
)  /*!50110 KEY_BLOCK_SIZE=1024; */

FAQs

  1. Why are comments important in a code?
    Comments make the code more human-readable. They can act as documentation and provide us with the complete detailing of the code.
     
  2. Do comments get executed along with the SQL statement?
    Comments don't get executed along with SQL statements unless they are executable comments that follow a special syntax in MySQL.
     
  3. Are SQL comments the same as comments in any other programming language?
    Yes, SQL comments are the same as comments in any other programming language as they all serve the same purpose, making code more readable and explanatory.
     
  4. Can multi-line comments be of one line?
    Yes, multi-line comments can be a line or even a word. Moreover, multi-line comments can also come between SQL statements.
     
  5. How do I view comments in SQL?
    You can use SQL command SHOW FULL COLUMNS as described in the MySQL manual. Its output contains the comments.

Key Takeaways

In this article, we discussed comments and their types. We also discussed their use, importance, and types of comment stylings in MySQL, along with their syntax and examples. After that, we saw executable comments in MySQL.

Recommended Readings:

You can also check out the Top 100 SQL Problems to get hands-on experience with frequently asked interview questions.
Refer to the Guided Path to learn more about DBMS. You can visit Coding Ninjas Studio to practice programming problems for your complete interview preparation and land your dream job.

Next article
SQL - CHECK Constraint
Live masterclass