Must I include all column attributes in an SQL statement when I want to alter only one?

会有一股神秘感。 提交于 2019-12-10 10:35:51

问题


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.



来源:https://stackoverflow.com/questions/5759667/must-i-include-all-column-attributes-in-an-sql-statement-when-i-want-to-alter-on

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!