Humpton
Humpton

Reputation: 1529

MySQL and comments

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.

Upvotes: 48

Views: 59727

Answers (11)

Gary Richardson
Gary Richardson

Reputation: 16441

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

Upvotes: 48

Binh Ho
Binh Ho

Reputation: 4993

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

Upvotes: 1

golimar
golimar

Reputation: 2548

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/

Upvotes: 0

Hebe
Hebe

Reputation: 810

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;

Upvotes: 4

Anjani Barnwal
Anjani Barnwal

Reputation: 1532

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

Upvotes: 3

Brad Kent
Brad Kent

Reputation: 5107

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

Upvotes: 8

Dieter Gribnitz
Dieter Gribnitz

Reputation: 5218

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.

Upvotes: -1

Stringent Software
Stringent Software

Reputation: 839

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.

Upvotes: 0

Edward Z. Yang
Edward Z. Yang

Reputation: 26762

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

Upvotes: 0

Robert Gamble
Robert Gamble

Reputation: 109182

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'

Upvotes: 13

micahwittman
micahwittman

Reputation: 12486

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;

Upvotes: 43

Related Questions