Mysql中change与modify的区别
官方文档介绍:
The
CHANGE
,MODIFY
, andALTER
clauses enable the names and definitions of existing columns to be altered. They have these comparative characteristics:
CHANGE
:
Can rename a column and change its definition, or both. # 修改column的name
Has more capability than
MODIFY
, but at the expense of convenience for some operations.CHANGE
requires naming the column twice if not renaming it. # 用法与modify不同With
FIRST
orAFTER
, can reorder columns. # 可以重置column的位置
MODIFY
:
Can change a column definition but not its name. # 仅仅能改变column的属性,不能改名!
More convenient than
CHANGE
to change a column definition without renaming it. # 比change方便呀,不需要写两次column nameWith
FIRST
orAFTER
, can reorder columns. # 也能重置column的位置
ALTER
: Used only to change a column default value. # 仅仅可以重置column的默认值
从官方文档可以发现modify和change的用法不同,相对而言modify更加方便。
change能修改column的名,而modify不行,其他都一样,所以平时用modify就可以,毕竟改column name的机会比较少。
使用方法,只例举change和modify,平常用modify就够了,打一次列名不费神。
Change:
ALTER table table_name
CHANGE 旧名 新名 varchar(50);
Modify:
ALTER table table_name
MODIFY 列名 varchar(50); # 不能修改名只能属性