mysql修改表结构包含但不限于以下情形:
修改表名,字段名,数据类型,默认值;
增加删除字段,索引和约束。
下面主要围绕以上进行示例!
添加列:
alter table 表名 add column 列名 数据类型 not null default ‘默认值’;
其中数据类型如int(5),not null,default都是可选项。
删除列:
alter table 表名 drop column 列名
修改列名name 为 address:
alter table 表名 change name address varchar(50)default null
修改列数据类型:
alter table 表名 modify 列名 bigint(20) not null
添加主键:
alter table 表名 add primary key (主键列名)
删除主键:
alter table 表名 drop primary key
添加普通索引:
alter table 表名 add index 索引名(列名)
添加唯一索引:
alter table 表名 add unique 索引名(列名)
删除索引:
alter table 表名 drop index 索引名;
修改表名:
rename table 原表名 to 新表名
移动表到另一个库:
rename db1.table1 to db2.table1
添加外键:
alter table add constraint 外键名 foreign key(外键列名)references 主表名(主表主键)