您的位置:首页 > 数据库 > Oracle

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

2015-06-19 16:31 441 查看
不小心把数据库好几张表的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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: