How can I add comments in MySQL?

MysqlDatabaseComments

Mysql Problem Overview


I want to add comment in SQL code. How can I do this? I'm using MySQL.

Mysql Solutions


Solution 1 - Mysql

Several ways:

# Comment
-- Comment
/* Comment */

Remember to put the space after --.

See the documentation.

Solution 2 - Mysql

"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;

Solution 3 - Mysql

You can use single-line comments:

-- this is a comment
# this is also a comment

Or a multiline comment:

/*
   multiline
   comment
*/

Solution 4 - Mysql

From here you can use:

#  For single line comments
-- Also for single line, must be followed by space/control character
/*
    C-style multiline comment
*/

Solution 5 - Mysql

/* comment here */

Here is an example:

SELECT 1 /* this is an in-line comment */ + 1;

Reference: 9.7 Comments

Solution 6 - Mysql

Three types of commenting are supported:

  1. Hash base single line commenting using #

    Select * from users ; # this will list users
    
  2. Double Dash commenting using --

    Select * from users ; -- this will list users
    

    Note: It's important to have single white space just after --

  3. Multi line commenting using /* */

    Select * from users ; /* this will list users */
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionamir amirView Question on Stackoverflow
Solution 1 - MysqlMartti LaineView Answer on Stackoverflow
Solution 2 - Mysqluser1178831View Answer on Stackoverflow
Solution 3 - MysqlfivedigitView Answer on Stackoverflow
Solution 4 - MysqlBortView Answer on Stackoverflow
Solution 5 - Mysqlrivethead_View Answer on Stackoverflow
Solution 6 - MysqlMr CoderView Answer on Stackoverflow