sql语句添加、修改字段以及索引(mysql)
在工作中,我们经常会遇到数据库表字段不够,修改扩展或者修改。
-- 修改字段名
alter table TABLE_NAME rename column A to B;
-- 修改字段类型(版本)
alter table TABLE_NAME alter column COLUMN_NAME COLUMN_TYPE not null ;
alter table TABLE_NAME modify column COLUMN_NAME COLUMN_TYPE not null ;
-- 添加字段类型
alter table TABLE_NAME add COLUMN_NAME COLUMN_TYPE not null default comment '';
-- 添加索引
alter table TABLE_NAME add index INDEX_NAME(`COLUMN_NAME`,[`COLUMN_NAME`]) ;
alter table TABLE_NAME add unique (`COLUMN_NAME`) ;
alter table TABLE_NAME add primary key (COLUMN_NAME) ;
-- 删除索引
drop index INDEX_NAME on TABLE_NAME ;
alter table TABLE_NAME drop index INDEX_NAME ;
alter table TABLE_NAME drop primary key ;