oracle 数据库缩减char类型 字段长度

来源:互联网 发布:武汉mac口红专柜哪里有 编辑:程序博客网 时间:2024/06/05 23:42

不小心把数据库好几张表的char类型的字段原长度1、2、3的都改为4了哭。增加长度后,数据库会自动填充空格,结果项目出问题了大哭。老老实实找方法改回来。

这里只贴出我的解决方法:

--如果要缩减长度的字段中含有 not null 的,需要进行步骤1、2、3、4、10,否则跳过。


--1导出表数据  

略去。。。
--2清空表数据
truncate table tbl_cust_if_seller;
--3将not null 字段  设为null
 alter table tbl_cust_if_seller modify is_signed_protocal    char(4) null;
--4导入表数据
略去。。。

--5添加表字段
alter table tbl_cust_if_seller add    is_signed_protocal_bak    char(4);
alter table tbl_cust_if_seller add    curcd1_bak                char(4);
alter table tbl_cust_if_seller add    curcd2_bak                char(4);
alter table tbl_cust_if_seller add    curcd3_bak                char(4);
alter table tbl_cust_if_seller add    curcd4_bak                char(4);
alter table tbl_cust_if_seller add    curcd5_bak                char(4);
alter table tbl_cust_if_seller add    short_section_curcd_bak   char(4);




--6为新增字段赋值
update tbl_cust_if_seller set
 is_signed_protocal_bak =  is_signed_protocal ,
 curcd1_bak             =  curcd1             ,
 curcd2_bak             =  curcd2             ,
 curcd3_bak             =  curcd3             ,
 curcd4_bak             =  curcd4             ,
 curcd5_bak             =  curcd5             ,
 short_section_curcd_bak=  short_section_curcd;
 
 --7修改原字段值为空
 update tbl_cust_if_seller set
is_signed_protocal =  '',
curcd1             =  '',
curcd2             =  '',
curcd3             =  '',
curcd4             =  '',
curcd5             =  '',
short_section_curcd=  '';
 
 --8修改原字段长度
alter table tbl_cust_if_seller modify  is_signed_protocal    char(1);
alter table tbl_cust_if_seller modify  curcd1                char(3);
alter table tbl_cust_if_seller modify  curcd2                char(3);
alter table tbl_cust_if_seller modify  curcd3                char(3);
alter table tbl_cust_if_seller modify  curcd4                char(3);
alter table tbl_cust_if_seller modify  curcd5                char(3);
alter table tbl_cust_if_seller modify  short_section_curcd   char(3);


--9原字段赋值
update tbl_cust_if_seller set
 is_signed_protocal =  substr(is_signed_protocal_bak ,1,1),
 curcd1             =  substr(curcd1_bak             ,1,3),
 curcd2             =  substr(curcd2_bak             ,1,3),
 curcd3             =  substr(curcd3_bak             ,1,3),
 curcd4             =  substr(curcd4_bak             ,1,3),
 curcd5             =  substr(curcd5_bak             ,1,3),
 short_section_curcd=  substr(short_section_curcd_bak,1,3);
 --10恢复not null
 alter table tbl_cust_if_seller modify is_signed_protocal    char(1) not null;
 
--11删除新增字段
alter table tbl_cust_if_seller drop (is_signed_protocal_bak   );
alter table tbl_cust_if_seller drop (curcd1_bak             );
alter table tbl_cust_if_seller drop (curcd2_bak             );
alter table tbl_cust_if_seller drop (curcd3_bak             );
alter table tbl_cust_if_seller drop (curcd4_bak               );
alter table tbl_cust_if_seller drop (curcd5_bak               );
alter table tbl_cust_if_seller drop (short_section_curcd_bak);


0 0
原创粉丝点击