https://www.cnblogs.com/wenlj/p/5258102.html
- 增加一个字段
代码如下 复制代码
//增加一个字段,默认为空
alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL;
//增加一个字段,默认不能为空
alter table user add COLUMN new2 VARCHAR(20) NOT NULL;
2.批量增加字段
1)使用事物
bagin; //事务开始
alter table em_day_data add f_day_house7 int(11);
alter table em_day_data add f_day_house8 int(11);
alter table em_day_data add f_day_house9 int(11);
alter table em_day_data add f_day_house10 int(11);
commit; //提交事务,事务结束
2)批量为表添加多个字段
alter table 表名 add (字段1 类型(长度),字段2 类型(长度),字段3 类型(长度));
alter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11));
- 增加字段到某字段之后
alter table hubble_vip_new_tips_allinfo_day add pop_click_cnt int(11) DEFAULT NULL after load_succ_cnt
alter table t_adviser_info add weight int comment "0轻仓,1重仓" after profit
- 删除一个字段
alter table user DROP COLUMN new2;
- 修改字段
//修改一个字段的类型
alter table user MODIFY new1 VARCHAR(10);
//修改一个字段的名称,此时一定要重新指定该字段的类型
alter table user CHANGE new1 new4 int;
6.批量修改字段名称
alter table 表 change 修改前字段名 修改后字段名称 int(11) not null,
change 修改前字段名 修改后字段名称 int(11) not null,
change 修改前字段名 修改后字段名称 int(11) not null,
change 修改前字段名 修改后字段名称 int(11) not null,
change 修改前字段名 修改后字段名称 int(11) not null
alter table em_day_data change f_day_house11 f_day_hour11 int(11) not null,
change f_day_house12 f_day_hour12 int(11) not null,
change f_day_house13 f_day_hour13 int(11) not null,
change f_day_house14 f_day_hour14 int(11) not null,
change f_day_house15 f_day_hour15 int(11) not null,
change f_day_house16 f_day_hour16 int(11) not null,
change f_day_house17 f_day_hour17 int(11) not null
- 添加注释
// 可以为表添加注释
ALTER TABLE `table_name` COMMENT'注释';
// 为字段添加注释,同样适用于修改
ALTER TABLE `table_name` CHANGE `column_name` `column_name` type(longth) UNSIGNED NULL DEFAULT NULL COMMENT '注释'
- 调整字段顺序
alter table 表名
change 字段名 新字段名 字段类型 默认值 after 字段名(跳到哪个字段之后)
alter table apptore_souapp_app_androidmarket;
change getPriceCurrency getPriceCurrency varchar(50) default null AFTER getPrice;
网友评论