mysql中alter语句中change和modify的区别
modify能修改字段类型和约束,而change不能。
change用来字段重命名,不能修改字段类型和约束;
modify不用来字段重命名,只能修改字段类型和约束;
试验比较:
1、字段重命名:
1)change
mysql>altertablet1changenumberidchar(2);
QueryOK,0rowsaffected(0.08sec)
Records:0Duplicates:0Warnings:0
2)modify
mysql>altertablet1modifyidnumint(2);
ERROR1064(42000):YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'numint(2)'atline1
mysql>
结论:能用change重命名,而modify不能。
2、修改字段类型和约束
1)modify
mysql>altertablet1modifyidint(2);
QueryOK,0rowsaffected(0.06sec)
Records:0Duplicates:0Warnings:0
mysql>altertablet1modifyidint(2)notnull;
QueryOK,0rowsaffected(0.08sec)
Records:0Duplicates:0Warnings:0
2)change
mysql>altertablet1changeidchar(2);
ERROR1064(42000):YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'char(2)'atline1
mysql>altertablet1changeidchar(2)notnull;
ERROR1064(42000):YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'char(2)notnull'atline1
结论:modify能修改字段类型和约束,而change不能。
最终结论:change用来字段重命名,不能修改字段类型和约束;
modify不用来字段重命名,只能修改字段类型和约束;