MySQL Notes
In this article, we will introduce MySQL comments. We will also understand what type of comments should be used and where.
Comments are written to describe the code and make it easier to understand. We also use comments to ignore a part of the code (do not let that part of the code be executed) while parsing SQL queries.
It also helps other programmers understand what is happening in your code. We will see how to use single and multi-line comments in MySQL. We will also learn about 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, using #
or -
.
Let us create a tb_teachers
table called to practice MySQL comments. We use #
to write single line comments in the MySQL query given below.
This comment is used at SQL
the end of the query and must be followed by a newline. If you do not use a newline after the comment, everything on the same line will be commented until a newline is encountered. It is not necessary #
to put a space after the query. However, #
it is a good idea to put a space after the query to increase readability.
#
Example code using MySQL:
# 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 do not put a newline after using single line comment. You can see that CREATE
the 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 use this type of comments in SQL queries as well. Refer to the following code; you can observe that we can use <pre> #
to comment in SQL queries.
# 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 us explore another way of single line commenting using the _ symbol. The following SQL code shows that we can use the double (dash) symbol for commenting -
even in SQL queries .-
This is the same as we use #
the em dash symbol for comments, with one difference. We must put at least one space after the second dash; otherwise, it will not work 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 multiple rows注释
If we need to explain the SQL query in detail to make the code easy to understand, we use multi-line comments. /* */
Everything inside will be ignored. It does not matter whether you put a line break at the end of such a comment, but it is better to have one to write 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 single line, but why do we have single line comments and add more work. It is better to use multi-line comments when you don't want to execute more than one line. 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 file comments
MySQL also supports executable comments. This type of comment provides you with portability between various databases. The code written in these comments will only be executed in MySQL. If you !
specify a version character after , it will only work with that specific MySQL version or higher.
Example code for executable annotations:
SELECT 3 /*! *2 */ AS MULTIPLICATION;
in conclusion
We have concluded that MySQL comments play a vital role while programming. It does not matter what type of programming it is. If you have to ignore multiple lines, using multi-line comments is a good choice; otherwise, single-line comments are fine. If you want a certain section of code to be executed only on the MySQL server, then you can use MySQL executable comments.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
If ELSE in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering
DATETIME vs. TIMESTAMP in MySQL
Publish Date:2025/04/11 Views:117 Category:MySQL
-
DATETIME and TIMESTAMP are two different data types that can be used to store values that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea
Execute multiple joins in one query in MYSQL
Publish Date:2025/04/11 Views:94 Category:MySQL
-
Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.
Joining 3 tables in MySQL
Publish Date:2025/04/11 Views:187 Category:MySQL
-
In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS
Use of UPDATE JOIN in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla
How to use the Row_Number() function in MySQL
Publish Date:2025/04/11 Views:142 Category:MySQL
-
In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti
Multiple primary keys in MySQL
Publish Date:2025/04/11 Views:66 Category:MySQL
-
In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b
Displaying foreign keys in MySQL
Publish Date:2025/04/11 Views:55 Category:MySQL
-
In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei
Select first N rows in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl