Must I include all column attributes in an SQL sta

2019-05-21 07:55发布

问题:

I have a MySQL database that, I would like to annotate by adding comments to all of the columns,

From what I read on the MySQL documentation, it is necessary to include the data type and all attributes when changing any single one of these.

For example, I must type

ALTER TABLE `dbname`.`tablename` 
  CHANGE COLUMN `columnname` `columnname` 
  INT(11) NULL DEFAULT NULL COMMENT 'this is my comment`; 

It would be much faster for me to avoid having to restate the column info for each change, for example, by only having to submit a command such as:

ALTER TABLE `dbname`.`tablename` 
 CHANGE COLUMN `columnname` 
 COMMENT 'this is my comment`; 

Are there any options for adding comments that do not require me to restate the table structure?

回答1:

The documentation is pretty clear that CHANGE COLUMN requires the full column definition:

CHANGE [COLUMN] old_col_name new_col_name column_definition

Anything optional would be in brackets.

Your best bet is probably to write a little one-off script to produce your ALTER TABLE commands based on the table's current schema. You should be able to extract the column definitions from whatever data access layer you're using.