MySQL and comments

Viewed 48334

Is it possible to add comments somehow, somewhere?

I don't pretend to be any sort of expert when using MySQL and certainly don't spend all day in it. More often than I would like I forget how I intend to use a column (usally the bit ones) and would be very excited if I could add a comment to remind me if 1 is good or bad, for example.

I'd be happy if it only showed up in something like 'show create table', but any obscure place within the table structures would be better and easier to find than the current post-it notes on my desk.

11 Answers

You can comment columns and tables:

CREATE TABLE example (
  example_column INT COMMENT="This is an example column",
  another_column VARCHAR COMMENT="One more column"
) TYPE=MYISAM COMMENT="This is a comment about table";

http://dev.mysql.com/doc/refman/5.0/en/create-table.html

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

Example:

CREATE TABLE foo (
  id int(10) NOT NULL auto_increment COMMENT 'unique ID for each foo entry',
  bar varchar(255) default NULL COMMENT 'the bar of the foo',
  ....
) TYPE=MyISAM;

MySQL supports comments on tables and columns which will show up on show create:

create table example (field1 char(3) comment 'first field') comment='example table'

Top voted answer worked for me only after equal sign removal. My working example would be

CREATE TABLE `example` (
`id` int(11) NOT NULL,
`two` varchar(255) COMMENT "comment text",
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Are you sure you're not looking to use an ENUM column instead? Good MySQL tables should be self-documenting.

An alternate approach would be to comment the schema files that have the SQL you use to define your tables (I assume you have those, and that you're not using PHPMyAdmin to grow table schemas on the fly...)

But if you insist, the INFORMATION_SCHEMA COLUMNS table, specifically the COLUMN_COMMENT column, is probably what you're looking for. It's proprietary MySQL syntax though, so I would tend to avoid it (although the idea of database interoperability is really a joke).

If you use the MySQL Administrator tool to manage/edit your databases, whenever you use the Table Editor, the comment for each column is automatically shown/editable.

Tested. this worked.

CREATE TABLE `table_with_comment` (
  `id` int(11) NOT NULL,
  `column_a` varchar(255) DEFAULT NULL COMMENT 'comment comlumn text',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='This is a table comment';
Related