MySQL Comments

Mehvish Ashiq Dec 10, 2021
  1. MySQL Single-Line Comments
  2. MySQL Multi-Line Comment
  3. MySQL Executable Comments
  4. Conclusion
MySQL Comments

In this article, we will introduce MySQL Comments. We will also learn where and what type of comment should be used.

Comments are written to describe the code, make it easy to understand. We also use comments to ignore a certain part of the code while parsing SQL queries (don’t let that piece of code be executed).

It also helps the other programmers to understand what is happening in code. We will see how we can use single-line and multi-line comments in MySQL. We will also have a look at the executable comments and their uses.

There are three different ways to write MySQL comments. We can use #, -, or /* and */ symbols to comment.

MySQL Single-Line Comments

We can use MySQL single-line comments in two ways, either by using # or -.

Let’s create a table named tb_teachers to practice MySQL comments. We use # to write a single-line comment in the MySQL query given below.

This comment is used at the end of the SQL query and must have a line break after it. If you do not use a line break after the comment, everything that comes on the same line will be commented until it encounters a line break. It is not required to put a white space after #. But it is a good approach to have a white space after # to increase readability.

MySQL Example Code Using #:

# Following SQL query will create a table named 'tb_teachers'
CREATE TABLE `practice_mysql_comments`.`tb_teachers` (
 TEACHER_ID	INTEGER NOT NULL,
 FIRST_NAME	VARCHAR(30) NOT NULL,
 LAST_NAME	VARCHAR(30) NOT NULL,
 GENDER	VARCHAR(30) NOT NULL,
 CITY	VARCHAR(64) NOT NULL,
 EMAIL	VARCHAR(64) NOT NULL,
 JOINING_YEAR INTEGER NOT NULL,
 PRIMARY KEY	(TEACHER_ID)
);

See the following code example where we don’t put a line break after using a single-line comment. You can see that the CREATE command is also partially commented.

# Following SQL query will create a table named 'tb_teachers' CREATE TABLE `practice_mysql_comments`.`tb_teachers` (
 TEACHER_ID	INTEGER NOT NULL,
 FIRST_NAME	VARCHAR(30) NOT NULL,
 LAST_NAME	VARCHAR(30) NOT NULL,
 GENDER	VARCHAR(30) NOT NULL,
 CITY	VARCHAR(64) NOT NULL,
 EMAIL	VARCHAR(64) NOT NULL,
 JOINING_YEAR INTEGER NOT NULL,
 PRIMARY KEY	(TEACHER_ID)
);

We can also use this type of comment within the SQL query. See the following piece of code; you can observe that we can comment within the SQL query using #.

# Following SQL query will create a table named 'tb_teachers' 
CREATE TABLE `practice_mysql_comments`.`tb_teachers` (
 TEACHER_ID	INTEGER NOT NULL, # teacher's id
 FIRST_NAME	VARCHAR(30) NOT NULL, # teacher's first name
 LAST_NAME	VARCHAR(30) NOT NULL, # teacher's last name
 GENDER	VARCHAR(30) NOT NULL, # teacher's gender
 CITY	VARCHAR(64) NOT NULL, # teacher's home town
 EMAIL	VARCHAR(64) NOT NULL, # teacher's email
 JOINING_YEAR INTEGER NOT NULL, # teacher's appointment year
 PRIMARY KEY (TEACHER_ID) # primay key of the teacher's table
);

Let’s explore the other way of single-line comments using the - symbol. The following SQL code shows that we can use the double ‘-’ (dash) symbol to comment even within the SQL query.

It is the same as we comment using the # symbol with only one difference. We have to put at least one white space after the second dash; otherwise, it will not act as a comment.

-- Following SQL query will create a table named 'tb_students' 
CREATE TABLE `practice_mysql_comments`.`tb_students` (
 STUDENT_ID	INTEGER NOT NULL, -- student's id
 FIRST_NAME	VARCHAR(30) NOT NULL, -- student's first name
 LAST_NAME	VARCHAR(30) NOT NULL, -- student;s last name
 GENDER	VARCHAR(30) NOT NULL, -- student's gender
 CITY	VARCHAR(64) NOT NULL, -- student's home town
 EMAIL	VARCHAR(64) NOT NULL, -- student's email
 REGISTRATION_YEAR INTEGER NOT NULL, -- student's registration year
 PRIMARY KEY (STUDENT_ID) -- primay key of the student's table
);

MySQL Multi-Line Comment

If we need to explain the SQL query in detail to make the code easy to understand, we use multi-line comments. Everything within the /* */ will be ignored. Either you put a line break at the end of this type of comment or not, it does not matter, but it is good to have a line break to write a clean and organized code.

/* 
Following SQL query will create a table named 'tb_students'
having the basic information about the students. This information includes
full name, gender, city, email and registration year.
*/
CREATE TABLE `practice_mysql_comments`.`tb_students` (
 STUDENT_ID	INTEGER NOT NULL, 
 FIRST_NAME	VARCHAR(30) NOT NULL, 
 LAST_NAME	VARCHAR(30) NOT NULL, 
 GENDER	VARCHAR(30) NOT NULL, 
 CITY	VARCHAR(64) NOT NULL, 
 EMAIL	VARCHAR(64) NOT NULL,
 REGISTRATION_YEAR INTEGER NOT NULL, 
 PRIMARY KEY (STUDENT_ID) 
);

Although we can treat multi-line comments as a single line, why increase the effort when we have single-line comments. It is good to use multi-line comments when you don’t want more than one line to be executed. See the following example.

/* 
Following SQL query will create a table named 'tb_students'
having the basic information about the students. This information includes
full name, gender, city, email and registration year.
*/
CREATE TABLE `practice_mysql_comments`.`tb_students` (
 STUDENT_ID	INTEGER NOT NULL, 
 FIRST_NAME	VARCHAR(30) NOT NULL, 
 LAST_NAME	VARCHAR(30) NOT NULL, 
 GENDER	VARCHAR(30) NOT NULL, 
 CITY	VARCHAR(64) NOT NULL, 
 /* EMAIL	VARCHAR(64) NOT NULL,
 REGISTRATION_YEAR INTEGER NOT NULL, */
 PRIMARY KEY (STUDENT_ID) 
);

MySQL Executable Comments

MySQL also supports executable comments. This type of comment gives you portability among various databases. The code written within these comments will only be executed in MySQL. If you specify version after the ! character, it will only work on that particular MySQL version or above that.

Example Code of Executable Comments:

SELECT 3 /*! *2 */ AS MULTIPLICATION;

Conclusion

We have concluded that MySQL comments play a vital role while programming. It does not matter what type of programming it is. Using multi-line comments is a good choice if you have to ignore more than one line; otherwise, a single-line comment is fine. You can use MySQL executable comments if you want a certain piece of code to be executed on MySQL Server only.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook