I want to add comment in SQL code. How can I do this? I'm using MySQL.
Asked
Active
Viewed 1.6e+01k times
6 Answers
256
Several ways:
# Comment
-- Comment
/* Comment */
Remember to put the space after --
.
See the documentation.

Peter Mortensen
- 30,738
- 21
- 105
- 131

Martti Laine
- 12,655
- 22
- 68
- 102
-
86[Remember to put the space](http://stackoverflow.com/q/14435989/632951) after `--` – Pacerier Mar 13 '15 at 11:20
-
1Is there any general best practice or style guideline for when to use these different syntaxes? Obviously the last one is ideal for multi-line comments, but is there any rule of thumb for single-line comments? – StockB Nov 21 '16 at 16:26
-
3@StockB no, but it never hurts to be consistent with your coding styles. – gdoron May 23 '17 at 08:01
33
"A comment for a column can be specified with the COMMENT
option. The comment is displayed by the SHOW CREATE TABLE
and SHOW FULL COLUMNS
statements. This option is operational as of MySQL 4.1. (It is allowed but ignored in earlier versions.)"
As an example
--
-- Table structure for table 'accesslog'
--
CREATE TABLE accesslog (
aid int(10) NOT NULL auto_increment COMMENT 'unique ID for each access entry',
title varchar(255) default NULL COMMENT 'the title of the page being accessed',
path varchar(255) default NULL COMMENT 'the local path of teh page being accessed',
....
) TYPE=MyISAM;

Smar
- 8,109
- 3
- 36
- 48
-
-
4It's what I was looking for :) Incidentally, I found the COMMENT argument had to be before any AFTER argument; order is important, evidently. – Soft Bullets Feb 09 '18 at 09:15
20
You can use single-line comments:
-- this is a comment
# this is also a comment
Or a multiline comment:
/*
multiline
comment
*/

Peter Mortensen
- 30,738
- 21
- 105
- 131

fivedigit
- 18,464
- 6
- 54
- 58
-
The `#` form is not accepted by [the syntax highlighter here](https://meta.stackexchange.com/questions/184108/what-is-syntax-highlighting-and-how-does-it-work/184109#184109) ("`lang-sql`"). Is it the syntax highlighter's fault or is that form special for MySQL? – Peter Mortensen Jan 31 '22 at 13:56
3
From here you can use:
# For single line comments
-- Also for single line, must be followed by space/control character
/*
C-style multiline comment
*/

Peter Mortensen
- 30,738
- 21
- 105
- 131

Bort
- 7,398
- 3
- 33
- 48
-
The `#` form is not accepted by [the syntax highlighter here](https://meta.stackexchange.com/questions/184108/what-is-syntax-highlighting-and-how-does-it-work/184109#184109) ("`lang-sql`"). Is it the syntax highlighter's fault or is that form special for MySQL? – Peter Mortensen Jan 31 '22 at 13:58
1
Three types of commenting are supported:
Hash base single line commenting using #
Select * from users ; # this will list users
Double Dash commenting using
--
Select * from users ; -- this will list users
Note: It's important to have single white space just after
--
Multi line commenting using /* */
Select * from users ; /* this will list users */

Peter Mortensen
- 30,738
- 21
- 105
- 131

Mr Coder
- 8,169
- 5
- 45
- 74
1
/* comment here */
Here is an example:
SELECT 1 /* this is an in-line comment */ + 1;
Reference: 9.7 Comments

Peter Mortensen
- 30,738
- 21
- 105
- 131

rivethead_
- 131
- 1
- 6