Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a MySQL database, and I have used MySQL Workbench to insert text into the "comment" fields of many of the columns and tables; the plan was to have the documentation stored in the database.

However, I do not know how to either a) add a comment to a column or, more importantly b) retrieve this information.

I have looked through the limited MySQL documentation on Describe, Show Columns, and Show Extensions.

For example, I would like to return this:

mysql> describe dogs;

+----------------+-------------------------------+
| Field          | Comment                       | 
+----------------+-------------------------------+
| id             | primary key                   | 
| Name           | Dog's name                    | 
| species_id     | Foreign key to look up species| 

Instead of

mysql> describe dogs;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment | 
| name           | varchar (255)| YES  | MUL | NULL    |                | 
| species_id     | int(11)      | YES  | MUL | NULL    |                |

Although this would do:

mysql> describe dogs;

mysql> describe dogs;
+----------------+--------------+------+-----+---------+----------------+-------------------------------+
| Field          | Type         | Null | Key | Default | Extra          | Comment                       | 
+----------------+--------------+------+-----+---------+----------------+-------------------------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |                               |
| name           | varchar (255)| YES  | MUL | NULL    |                | Dog's name                    |  
| species_id     | int(11)      | YES  | MUL | NULL    |                | Foreign key to look up species|

What are my options? Does MySQL workbench export such documentation?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You can do this with normal DML/DDL.

Create a table with comments:

mysql> create table testcomments ( 
    -> col1 integer comment 'comment on integer column',
    -> col2 varchar(20) comment 'comment on varchat col');
Query OK, 0 rows affected (0.01 sec)

Select the information from the data dictionary (documentation link):

mysql> select column_name, column_comment
    -> from INFORMATION_SCHEMA.COLUMNS
    -> where TABLE_NAME = 'testcomments';
+-------------+---------------------------+
| column_name | column_comment            |
+-------------+---------------------------+
| col1        | comment on integer column |
| col2        | comment on varchat col    |
+-------------+---------------------------+
2 rows in set (0.00 sec)

mysql>

Add/modify a comment:

mysql> alter table testcomments
    -> change column col1
    -> col1 integer comment 'modified comment on integer column';

Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select column_name, column_comment  from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'testcomments';
+-------------+------------------------------------+
| column_name | column_comment                     |
+-------------+------------------------------------+
| col1        | modified comment on integer column |
| col2        | comment on varchat col             |
+-------------+------------------------------------+
2 rows in set (0.00 sec)

mysql>

Modify a table comment:

mysql> alter table testcomments comment 'foo';
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>

View a table comment:

mysql> select TABLE_COMMENT
    -> from INFORMATION_SCHEMA.TABLES
    -> where TABLE_NAME = 'testcomments';
+---------------+
| TABLE_COMMENT |
+---------------+
| foo           |
+---------------+
1 row in set (0.00 sec)

mysql>
share|improve this answer
    
Thanks for your help. I never knew what INFORMATION_SCHEMA is, but how should I edit the table comment (e.g. info in INFORMATION_SCHEMA.TABLE_COMMENT?) –  David Sep 19 '12 at 19:27
    
The info is already there in my answer "Add/modify a comment" –  Phil Sep 19 '12 at 19:32
    
I see where the column comment is added, but not where the table comment is added. –  David Sep 19 '12 at 19:40
    
Sorry, I misread. Answer edited. –  Phil Sep 19 '12 at 19:44
    
Thanks - that is just what I needed, next on to tex.stackexchange.com/q/73241/1783 –  David Sep 19 '12 at 20:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.