[MySQL] 用alter table语句修改表的结构

用alter table语句修改表的结构

1. 增加列

alter table tbl_name add col_name type

例如,  给pet的表增加一列 weight,

mysql>alter table pet add weight int;

2. 删除列

alter table tbl_name drop col_name

例如, 删除pet表中的weight这一列

mysql>alter table pet drop weight;

3. 改变列, 分为改变列的属性和改变列的名字

  改变列的属性,方法1

alter table tbl_name modify col_name type

例如, 改变weight的类型

mysql>alter table pet modify weight varchar(30);

    改变列的属性, 方法2

alter table tbl_name change old_col_name col_name type

例如, 改变weight的类型

alter table pet change weight weight varchar(30);

    改变列的名字

alter table tbl_name change old_col_name col_name

例如改变pet表中weight的名字

mysql>alter table pet change weight wei;

4. 改变表的名字

alter table tbl_name rename new_tbl

例如, 把pet表更名为animal

mysql>alter table pet rename animal;
posted @ 2011-08-11 09:20  ShanHaiyang  阅读(512)  评论(0编辑  收藏  举报