oracle 数据库缩减char类型 字段长度
2015-06-19 16:31
441 查看
不小心把数据库好几张表的char类型的字段原长度1、2、3的都改为4了
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cry.gif)
。增加长度后,数据库会自动填充空格,结果项目出问题了
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif)
。老老实实找方法改回来。
这里只贴出我的解决方法:
--如果要缩减长度的字段中含有 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);
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cry.gif)
。增加长度后,数据库会自动填充空格,结果项目出问题了
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif)
。老老实实找方法改回来。
这里只贴出我的解决方法:
--如果要缩减长度的字段中含有 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);
相关文章推荐
- Oracle 数据类型转换
- Oracle 10g/11g 导入导出整理
- oracle 表查询(2)
- oracle top 语句(转)
- Oracle union用法
- Msdao15调用oracle存储过程
- Oracle10g 回收站及彻底删除table
- Oracle中TO_DATE格式
- [DB] oracle删除重复数据
- 浅谈Oracle中的物化视图
- Oracle物化视图的一般使用
- oracle rac环境下,修改参数cluster_database导致的ORA-29707与ORA-01102
- 基于oracle的sql优化方法论
- Install oracle 12c
- Oracle 尽量避免在 SQL语句中使用 LIKE
- Oracle 尽量避免在 SQL语句的WHERE子句中使用函数
- 本地使用Plsql连接远程服务器上oracle!
- Java并发教程(Oracle官方资料)
- ORACLE in与exists语句的区别
- Oracle 尽量避免在SQL语句中使用 OR