My SQL comment while creating table

Viewed 4757

Is there anyway to add comment/description to a MYSql table, while creating it? The purpose is to use the information to inform other users about the purpose/use of the table in the software. I know that we can put comments to a column, while creating table,but nothing for the same is available anywhere.

Any help shall be deeply appreciated. Also, do share the query to retrieve the info/comment/description from the table.

1 Answers

Had you checked out mysql's manual on create table statement, you would have found that you can add a comment clause to the end of the statement:

table_option: AUTO_INCREMENT [=] value | AVG_ROW_LENGTH [=] value | [DEFAULT] CHARACTER SET [=] charset_name | CHECKSUM [=] {0 | 1} | [DEFAULT] COLLATE [=] collation_name | COMMENT [=] 'string'

...

COMMENT

A comment for the table, up to 2048 characters long.

...

The comment is displayed as part of the ouput of SHOW CREATE TABLE. The text of the comment is also available as the TABLE_COMMENT column of the MySQL Information Schema TABLES table.

This comment syntax is also supported with ALTER TABLE statements for NDB tables. Keep in mind that a table comment used with ALTER TABLE replaces any existing comment which the table might have had perviously.

CREATE TABLE t1 (
    c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    c2 VARCHAR(100),
    c3 VARCHAR(100) )
ENGINE=INNODB
COMMENT='Whatever'
Related