50

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.

0

11 Answers 11

49

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";
Sign up to request clarification or add additional context in comments.

2 Comments

COMMENT [=] 'string' is valid syntax for the table option, but for column comments is only [COMMENT 'string'] at least as far back as MySQL 5.0. dev.mysql.com/doc/refman/8.0/en/create-table.html
The query won't execute since MySQL requires a maximum length for the VARCHAR column.
44

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;

Comments

13

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'

Comments

8

This is an oldie, and there many response on how to update a columns comment, or create a table with comments. But the given answers on how to view the comments are rather awful

The easiest way to view the comments is via SHOW COLUMNS with the FULL keyword:
SHOW FULL COLUMNS FROM mytable

Comments

4

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;

Comments

2

if you want comment in table (in phpmyadmin) then follow these steps

  1. open localhost/phpmyadmin
  2. goto your database and select table
  3. now select operations menu from top.
  4. and goto table options and edit table comments. enter image description here

Comments

1

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

Comments

0

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).

1 Comment

You know, I might be. But, my low-level of expertise has not translated into much success with the whole ENUM thing. I do like to have 'good' tables so I'll think about ENUMs some more sometime soon.
0

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.

1 Comment

I don't. I'm a low skill level command line creator * PHP enter / change / extract data type user.
0

If you reached here looking for comments in DATABASE/SCHEMA, it's not supported in MySQL but it is in MariaDB:

https://mariadb.com/kb/en/create-database/

Comments

-1

I just wrote an app for this.

Can be found here: https://github.com/SplicePHP/mysql-comments

Allows you to to update multiple database tables and columns in a single view.

Instructions in link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.