Alter MySQL table to add comments on columns
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 tableyour_column
with the name of your commentyour_previous_column_definition
with the column's column_definition, which I recommend getting via aSHOW 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.
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
- Export all to a CSV
- 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.
try:
ALTER TABLE `user` CHANGE `id` `id` INT( 11 ) COMMENT 'id of user'