Introduction
In this chapter, we will learn about comments in MySQL. Comments are used to document SQL code, making it easier to understand and maintain. They can provide explanations, annotate complex queries, or temporarily disable parts of the code. We will cover the syntax for adding comments, examples of their usage, and important considerations for using comments in MySQL.
Types of Comments
MySQL supports three types of comments:
- Single-line comments: These comments begin with
--or#and continue to the end of the line. - Multi-line comments: These comments are enclosed between
/*and*/and can span multiple lines.
Syntax
Single-line Comments
There are two ways to write single-line comments in MySQL:
-- This is a single-line comment
SELECT * FROM employees;
# This is another single-line comment
SELECT * FROM departments;
Multi-line Comments
Multi-line comments are written as follows:
/*
This is a multi-line comment
that spans multiple lines
*/
SELECT * FROM employees;
Examples
Let’s go through some examples where we use comments in SQL queries to document the code.
- Single-line Comments:
-- Select all columns from the employees table
SELECT * FROM employees;
# Select all columns from the departments table
SELECT * FROM departments;
- Multi-line Comments:
/*
Retrieve the first name and last name
of all employees from the employees table
*/
SELECT first_name, last_name
FROM employees;
Full Example
Let’s go through a complete example where we create a database and tables, and use comments to document the SQL code.
- Create a Database:
-- Create a new database named company
CREATE DATABASE company;
- Select the Database:
-- Select the company database for use
USE company;
- Create the Departments and Employees Tables:
/* Create the departments table
with columns for department_id and department_name */
CREATE TABLE departments (
department_id INT PRIMARY KEY AUTO_INCREMENT,
department_name VARCHAR(100) NOT NULL
);
/* Create the employees table
with columns for employee_id, first_name, last_name, and department_id */
CREATE TABLE employees (
employee_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
- Insert Data into the Departments and Employees Tables:
-- Insert data into the departments table
INSERT INTO departments (department_name) VALUES
('Sales'), ('Marketing'), ('HR'), ('IT'), ('Finance');
-- Insert data into the employees table
INSERT INTO employees (first_name, last_name, department_id) VALUES
('Rahul', 'Sharma', 1),
('Priya', 'Singh', 2),
('Amit', 'Kumar', 3),
('Neha', 'Verma', 1),
('Sahil', 'Mehta', 4);
- Query the Employees Table:
-- Select all columns from the employees table
SELECT * FROM employees;
Output
| employee_id | first_name | last_name | department_id |
|---|---|---|---|
| 1 | Rahul | Sharma | 1 |
| 2 | Priya | Singh | 2 |
| 3 | Amit | Kumar | 3 |
| 4 | Neha | Verma | 1 |
| 5 | Sahil | Mehta | 4 |
In this example, comments are used to document each step of the SQL code, making it easier to understand and maintain.
Important Considerations
-
Performance: Comments do not affect the performance of SQL queries. They are ignored by the MySQL parser.
-
Readability: Use comments to improve the readability of your SQL code. Well-documented code is easier to understand and maintain.
-
Commenting Out Code: You can use comments to temporarily disable parts of your SQL code during development or debugging.
-- SELECT * FROM employees WHERE department_id = 1;
Conclusion
Comments are essential for documenting SQL code in MySQL. This chapter covered the syntax for adding single-line and multi-line comments, provided examples of their usage, and discussed important considerations.