用一条ALTER语句实现。
CHANGE:如果我们不只是修改单一列,而是用一条语句改变两个列,我们需要修改列的名称,同时更改他们的数据类型,这时就需要我们用到关键字CHANGE,可以在一条语句中放入多个CHANGE,在中间加上分隔的逗号即可。
比如原来的表两列,类型为VARCHAR(50),和VARCHAR(10),现在不止要更改列名,还要更改这两列的类型。
程序如下:
MODIFY:使用它可以只修改列的类型而不会干涉它的名称,假设要把proj_desc列的字符长度修改为VARCHAR(120)以容纳更多的说明文字,只要这么做就可以。
总结如下
(1)既更改列名也更改类型,用CHANGE
(2)只修改类型,用MODIFY,但这种方法比较慢,我们用另一种方法直接修改.frm文件,而不改动表本身。
mysql 中alter语句中change和modify的区别modify能修改字段类型和约束,而change不能。
change用来字段重命名,不能修改字段类型和约束;
modify不用来字段重命名,只能修改字段类型和约束;
试验比较:
1、字段重命名:
1)change
mysql>alter table t1 change number id char(2)
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
2)modify
mysql>alter table t1 modify id num int(2)
ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'num int(2)' at line 1
mysql>
结论:能用change重命名,而modify不能。
2、修改字段类型和约束
1)modify
mysql>alter table t1 modify id int(2)
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>alter table t1 modify id int(2) not null
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
2)change
mysql>alter table t1 change id char(2)
ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'char(2)' at line 1
mysql>alter table t1 change id char(2) not null
ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'char(2) not null' at line 1
结论:modify能修改字段类型和约束,而change不能。
最终结论:change用来字段重命名,不能修改字段类型和约束;
modify不用来字段重命名,只能修改字段类型和约束;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)