MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
Comments in MySQL are used to explain sections of SQL statements, or to prevent execution of SQL commands.
In MySQL, there are three ways to include comments:
#
comment: The #
symbol can be used to start a single-line comment. Everything from the #
symbol to the end of the line is considered a comment and is ignored by MySQL.
Example:
# This is a single-line comment SELECT * FROM users;
--
comment: The --
(double-dash with space) can also be used to start a single-line comment. Everything from the --
to the end of the line is considered a comment and is ignored by MySQL.
Example:
-- This is another single-line comment SELECT * FROM users;
/*...*/
comment: The /*...*/
symbols can be used to start and end a multi-line comment. Everything between /*
and */
is considered a comment and is ignored by MySQL.
Example:
/* This is a multi-line comment */ SELECT * FROM users;
Note: Be careful when commenting out multiple lines with #
or --
as they only comment out a single line.
In general, comments can be very useful for explaining your code to others, or leaving notes to yourself for future reference. They can also be helpful for debugging, by temporarily disabling certain lines of code.
Using Single-Line Comments in MySQL Scripts:
--
and extend to the end of the line.-- This is a single-line comment SELECT * FROM mytable;
Adding Comments to SQL Statements in MySQL:
--
to annotate specific SQL statements.SELECT column1, column2 -- Selecting specific columns FROM mytable;
Commenting Out Code in MySQL Queries:
-- SELECT * FROM mytable; -- This line is commented out
Multi-Line Comment Examples in MySQL:
/*
to start and */
to end, allowing comments spanning multiple lines./* This is a multi-line comment spanning multiple lines */ SELECT * FROM mytable;
Disabling Parts of SQL Queries with Comments in MySQL:
SELECT * -- , column2 FROM mytable;
MySQL Comments in Stored Procedures and Functions:
DELIMITER // CREATE PROCEDURE myprocedure() BEGIN -- Procedure logic here END // DELIMITER ;