Modify column Vs change column

眉间皱痕 提交于 2019-11-26 12:41:47

问题


I know, we can not rename a column using modify column syntax,but can change column syntax.

My question is: what is the main usage of modify syntax?

For example,

alter table tablename change col1 col1 int(10) not null

instead of

alter table tablename modify col1 int(10) not null



Edited
Question replaced

What is the main usage of modify syntax?

Above question was replaced by below

Why we have to use change column instead of modify column?


回答1:


CHANGE COLUMN If you have already created your MySQL database, and decide after the fact that one of your columns is named incorrectly, you don't need to remove it and make a replacement, you can simply rename it using change column.

ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL FIRST;

MODIFY COLUMN This command does everything CHANGE COLUMN can, but without renaming the column.You can use the modify SQL command if you need to resize a column in MySQL. By doing this you can allow more or less characters than before. You can't rename a column using modify and other

ALTER TABLE MyTable MODIFY COLUMN foo VARCHAR(32) NOT NULL AFTER baz;

Note : ALTER TABLE is used for altering a table means to change column name, size, drop column. CHANGE COLUMN and MODIFY COLUMN commands cannot be used without help of ALTER TABLE command.




回答2:


The difference is whether you want to change the column name, column definition or both.

CHANGE

Can rename a column or change its definition, or both.

ALTER TABLE t1 CHANGE a b BIGINT NOT NULL

MODIFY

Can change a column definition but not its name

ALTER TABLE t1 MODIFY b INT NOT NULL

RENAME COLUMN

Can change a column name but not its definition.

ALTER TABLE t1 RENAME COLUMN b TO a


You can check the docs for the complete explanation.




回答3:


I found one difference after more than an hour of effort in trying to make a non auto_increment column into auto_increment statement: alter table doctor_experience modify column id int(11) unsigned auto_increment works, but statment: alter table doctor_experience change column id id int(11) unsigned auto_increment will report an error.




回答4:


That is the same. It was done to support another syntax (Oracle ALTER TABLE as I know). You can use both of them.

Note: ALTER TABLE CHANGE old_col_name new_col_name syntax allows renaming column using one command.



来源:https://stackoverflow.com/questions/14767174/modify-column-vs-change-column

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