修改表结构信息

1.如果想在一个已经建好的表中添加一列,可以用诸如:

alter table t1 add column ctime timestamp default current_timestamp;

 //在t1表中添加了一列用于存储时间,另外添加的该列在表的最后一列的位置,如果我们希望添加在指定的位置,可以用:

alter table t1 add column ctime timestamp default current_timestamp alter utime;

 //这个命令的意思是添加ctime到utime这一列后面,如果想添加到第一列的话,可以用:

alter table t1 add column addc varchar(30) not null first;

 2.修改表中的列名

将表t1中,列名def改为unit

alter table t1 change def unit char;

 3.修改列的类型

alter table t1 modify def varchar(100);
//或者 alter table t1 change def def varchar(100);

 4.删除t1表中的def列

alter table t1 drop column del;

 5.添加主键

alter table t1 add new_field_id int(10) unsigned default 0 not null auto_increment, add primary key(new_field_id);

 6.重命名表

alter table t1 rename t2;

 

猜你喜欢

转载自lylan.iteye.com/blog/2184968