136

I have been checking the MySQL Documentation for ALTER TABLE and it does not seem to include a way to add or modify a comment to a column. How can I do this?

-- for table
ALTER TABLE myTable COMMENT 'Hello World'

-- for columns
-- ???
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103

5 Answers5

169

try:

 ALTER TABLE `user` CHANGE `id` `id` INT( 11 ) COMMENT 'id of user'  
Rufinus
  • 29,200
  • 6
  • 68
  • 84
  • 20
    It seems to work fine, but is there any other way to do it without including the column definition ? – Jhonny D. Cano -Leftware- Jan 29 '10 at 14:23
  • i played around, but it didnt work. this was the minimum set, maybe there is some other way. – Rufinus Jan 29 '10 at 14:49
  • 21
    This solution can break the auto increments. – workdreamer Jul 31 '13 at 12:10
  • 1
    Updating the column_comment field in the information_schema.columns table (for the appropriate table_schema/table_name/column_name combination) would preclude the need for column definitions and prevent issues with auto increments. – Marcus Pope Mar 04 '14 at 23:34
  • 17
    Note that altering a comment will cause a full resconstruction of the table. So you may choose to live without it on very big table. – Courtney Miles May 29 '14 at 22:50
  • 2
    @MarcusPope it's impossible. See http://dba.stackexchange.com/questions/78268/updating-mysql-trigger-via-information-schema-access-denied – gaRex Nov 11 '15 at 08:12
  • 1
    Oracle can do it easy:-) – powder366 Jun 29 '16 at 15:32
  • 1
    @MarcusPope http://stackoverflow.com/q/30912394/1883256 `#1044 - Access denied for user 'root'@'localhost' to database 'information_schema' ` – Pathros Mar 27 '17 at 16:34
  • 8
    @user2045006 That is not (or no longer) true, as long as the column definition matches the existing definition exactly. Comments can be added without causing table reconstruction. – Torben Apr 04 '17 at 06:34
  • @user2045006 things change in 7 years :) – Rufinus Apr 04 '17 at 07:17
  • 1
    It is error prone and absolutely horrible to have to re-specify the original definition, and must be the reason you so rarely see documentation on database tables and columns. – Micah Elliott Aug 12 '21 at 21:22
  • I think that if the re-specific original definition matches, the table will not be reconstructed. On the hand, if not matched, the table will be reconstructed. – a2htray yuen Apr 25 '22 at 06:03
  • Does anyone know how to do the same for a column that has VARCHAR2() data type? – Vivarsh Aug 07 '23 at 14:44
54

You can use MODIFY COLUMN to do this. Just do...

ALTER TABLE YourTable
MODIFY COLUMN your_column
your_previous_column_definition COMMENT "Your new comment"

substituting:

  • YourTable with the name of your table
  • your_column with the name of your comment
  • your_previous_column_definition with the column's column_definition, which I recommend getting via a SHOW CREATE TABLE YourTable command and copying verbatim to avoid any traps.*
  • Your new comment with the column comment you want.

For example...

mysql> CREATE TABLE `Example` (
    ->   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    ->   `some_col` varchar(255) DEFAULT NULL,
    ->   PRIMARY KEY (`id`)
    -> );
Query OK, 0 rows affected (0.18 sec)

mysql> ALTER TABLE Example
    -> MODIFY COLUMN `id`
    -> int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Look, I''m a comment!';
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE Example;
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                  |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Example | CREATE TABLE `Example` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Look, I''m a comment!',
  `some_col` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

* Whenever you use MODIFY or CHANGE clauses in an ALTER TABLE statement, I suggest you copy the column definition from the output of a SHOW CREATE TABLE statement. This protects you from accidentally losing an important part of your column definition by not realising that you need to include it in your MODIFY or CHANGE clause. For example, if you MODIFY an AUTO_INCREMENT column, you need to explicitly specify the AUTO_INCREMENT modifier again in the MODIFY clause, or the column will cease to be an AUTO_INCREMENT column. Similarly, if the column is defined as NOT NULL or has a DEFAULT value, these details need to be included when doing a MODIFY or CHANGE on the column or they will be lost.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
18

Script for all fields on database:

SELECT 
table_name,
column_name,
CONCAT('ALTER TABLE `',
        TABLE_SCHEMA,
        '`.`',
        table_name,
        '` CHANGE `',
        column_name,
        '` `',
        column_name,
        '` ',
        column_type,
        ' ',
        IF(is_nullable = 'YES', '' , 'NOT NULL '),
        IF(column_default IS NOT NULL, concat('DEFAULT ', IF(column_default IN ('CURRENT_TIMESTAMP', 'CURRENT_TIMESTAMP()', 'NULL', 'b\'0\'', 'b\'1\''), column_default, CONCAT('\'',column_default,'\'') ), ' '), ''),
        IF(column_default IS NULL AND is_nullable = 'YES' AND column_key = '' AND column_type = 'timestamp','NULL ', ''),
        IF(column_default IS NULL AND is_nullable = 'YES' AND column_key = '','DEFAULT NULL ', ''),
        extra,
        ' COMMENT \'',
        column_comment,
        '\' ;') as script
FROM
    information_schema.columns
WHERE
    table_schema = 'my_database_name'
ORDER BY table_name , column_name
  1. Export all to a CSV
  2. Open it on your favorite csv editor

Note: You can improve to only one table if you prefer

The solution given by @Rufinus is great but if you have auto increments it will break it.

Umbrella
  • 4,733
  • 2
  • 22
  • 31
workdreamer
  • 2,836
  • 1
  • 35
  • 37
  • 3
    According to a new user that doesn't have enough rep to comment, `dump.aux_comment,` needs to be `column_comment,`. Would you mind checking whether it is true? – nhahtdh Sep 11 '13 at 22:14
  • Sorry for the mistake. – workdreamer Sep 12 '13 at 11:06
  • Of course, if you have an id auto_increment, you need to do `ALTER TABLE MODIFY id INT NOT NULL AUTO_INCREMENT COMMENT 'id of user';`. This don't break auto increments. – mold May 06 '17 at 23:18
  • @workdreamer I was refering to Rufinus solution that you say it could break the auto increment. No, the given solution doesn't break it. – mold May 08 '17 at 18:56
  • If you use the solution given by @rufinus as it is, it will break, only if they use your solution it will not break. – workdreamer May 08 '17 at 21:30
  • 2
  • note for anyone who will be using this in mysql8.0: extras column got updated & it has two new possible values [STORED GENERATED, DEFAULT_GENERATED] which if added in modify query will throw error. – Bhavay Anand Apr 23 '21 at 13:08
4

The information schema isn't the place to treat these things (see DDL database commands).

When you add a comment you need to change the table structure (table comments).

From MySQL 5.6 documentation:

INFORMATION_SCHEMA is a database within each MySQL instance, the place that stores information about all the other databases that the MySQL server maintains. The INFORMATION_SCHEMA database contains several read-only tables. They are actually views, not base tables, so there are no files associated with them, and you cannot set triggers on them. Also, there is no database directory with that name.

Although you can select INFORMATION_SCHEMA as the default database with a USE statement, you can only read the contents of tables, not perform INSERT, UPDATE, or DELETE operations on them.

Chapter 21 INFORMATION_SCHEMA Tables

mold
  • 1,012
  • 7
  • 17
-3

As per the documentation you can add comments only at the time of creating table. So it is must to have table definition. One way to automate it using the script to read the definition and update your comments.

Reference:

http://cornempire.net/2010/04/15/add-comments-to-column-mysql/

http://bugs.mysql.com/bug.php?id=64439

Nageswara Rao
  • 954
  • 1
  • 10
  • 32
  • 3
    This answer is wrong (it's possible to update the comments after table creation even if it's tedious), and it only links to other websites, so it's not useful in terms of a Stack Overflow answer. – Lukas Eder Jan 04 '18 at 13:01