MySQL Comments
In this page:
-- single-line comment
# MySQL-style single-line comment
/* multi-line
comment */
Single-Line Comments with Double Dash
A double-dash (--) starts a comment that runs to the end of the line, but standard SQL requires a space immediately after the dashes or MySQL may not recognize it as a comment at all.
Example: Single-Line Comments with Double Dash
-- This is a comment
SELECT 1;
Single-Line Comments with Hash
The hash symbol (#) is a MySQL-specific single-line comment marker that behaves like the double-dash but, unlike it, doesn't require a trailing space — a small but easy-to-forget difference between the two styles.
Example: Single-Line Comments with Hash
#This is a MySQL-specific comment
SELECT 1;
Multi-Line Comments
Block comments start with /* and end with */, and can span multiple lines, which makes them the right choice for longer explanations of why a complex query is structured the way it is.
Example: Multi-Line Comments
/* This is a
multi-line comment */
SELECT 1;
Comments inside SQL Statements
You can drop a block comment mid-statement, right next to a column name or clause, to document exactly what that piece does without breaking the SQL syntax around it.
Example: Comments inside SQL Statements
SELECT name /* the customer's full name */ FROM customers;
Executable Comments
MySQL supports a special executable comment syntax (/*! ... */) that other database systems simply ignore as a normal comment, letting you write MySQL-specific optimizations that stay portable to other engines.
Example: Executable Comments
SELECT /*! SQL_NO_CACHE */ * FROM customers;
- Writing
--commentwithout a space after the two dashes, when MySQL requires--with a space (or a control character) to start a comment. - Forgetting the closing
*/on a block comment, so the rest of the script is commented out. - Putting a comment before a
;in a way that hides part of the statement, likeSELECT 1 -- ;.
- MySQL is a relational database management system that stores data in tables and uses SQL to work with it.
- Installing MySQL and using Workbench or the command line lets you run SQL statements.
- Comments and an overview of data types are the first building blocks for writing MySQL code.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: