MySQL database platform allows you to write comments in the .sql
query file.
Comments in MySQL query can be either single or multiple lines, depending on what comment syntax you use in your file.
For single-line comments, you can use the following syntax:
--
double dash symbol followed by a space#
a hash symbol
Here’s an example of single-line comments in MySQL:
USE school_db; -- Use the database called school_db
SELECT * FROM students; # Select all from students table
Please note that you need to add a space after the double dash symbol for commenting with the double dash.
You don’t need to add a space if you’re using the hash symbol for comment.
You can also put an inline comment using the forward-slash and asterisk symbol (/*
) followed by the closing tag asterisk and forward slash (*/
) as follows:
/* This is also a valid MySQL comment */
The inline comment can be put in the middle of a query as shown below:
SELECT id, first_name /* , last_name */ FROM students;
The query above will run and fetch only the id
and first_name
column, ignoring the last_name
column.
You can also create a multi-line comment using the forward-slash and asterisk symbol as follows:
/*
* Author: Nathan Sebhastian
* Date: 29th Sep 2021
*
* This query file is for students table
*
*/
SELECT * FROM students;
A multi-line comment is commonly used to inform developers about the specs of the query file.
And that’s how you can write comments in MySQL 😉