ALTER TABLE tb_name ADD [ COLUMN ] 新字段名数据类型 [约束条件] [FIRST | AFTER 已有字段名];
示例
1
ALTER TABLE `wno704_tbl` ADD COLUMN `status` INT FIRST;
3.2 修改字段
语法
1 2 3
ALTER TABLE tb_name CHANGE [COLUMN] 原字段名新字段名数据类型 [约束条件]; ALTER TABLE tb_name ALTER [COLUMN] 字段名 {SET | DROP} DEFAULT; ALTER TABLE tb_name MODIFY [COLUMN] 字段名数据类型 [约束条件] [FIRST | AFTER 已有字段名];
示例
1 2 3
ALTER TABLE `wno704_tbl` CHANGE COLUMN `status` `flag` INT; ALTER TABLE `wno704_tbl` ALTER COLUMN `status` SET DEFAULT 0; ALTER TABLE `wno704_tbl` MODIFY COLUMN `status` INT AFTER `date`;
3.3 删除字段
语法
1
ALTER TABLE tb_name DROP [COLUMN] 字段名;
示例
1
ALTER TABLE `wno704_tbl` DROP COLUMN `flag`;
四、表操作
4.1 重命名表
语法
1 2
ALTER TABLE 原表名 RENAME [TO] 新表名; RENAME TABLE 原表名1 TO 新表名1 [, 原表名2 TO 新表名2] ... ...;
示例
1 2
ALTER TABLE `wno704_tbl` RENAME TO `wno704_tb`; RENAME TABLE `wno704_tb` TO `wno704_tbl`;
4.2 删除表
语法
1
DROP TABLE [IF EXISTS] 表1 [, 表2]...;
示例
1
DROP TABLE IF EXISTS `wno704_tbl`;
4.3 insert into select
语法
1
insert into TABLE 表1 ([COLUMN]) select [COLUMN] from 表2;
示例
1 2 3
insert into total_index_td(date,orgid,type,num,update_time) select date_format(now(), '%Y-%m') date,p.orgid,15 type,0 num,now() update_time from entry_pianquzb_report p where p.attr = 2 and p.delflag = 0 and not exists (select 1 from total_index_td q where q.orgid = p.orgid and q.type = 15);
4.4 注解分析
1 2 3 4 5
SELECT TABLE_NAME,COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE , COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'kdptdb' and TABLE_NAME = 'entry_pianquzb_report';
ALTER TABLE kdptdb.entry_pianquzb_report MODIFY COLUMN latn int COMMENT '地市';