在修改表字段的NUMBER类型的精度或刻度时,你可能会遇到
ORA-01440: column to be modified must be empty to decrease precision or scale,
下面介绍一下,如何处理这个问题。测试案例如下:
drop table test purge;
create table test(product_id number, price number(38,1));
insert into test
select 1001, 18.2 from dual union all
select 1002, 38.5 from dual union all
select 1003, 34.8 from dual union all
select 1004, 87.4 from dual;
commit;
select * from test;
alter table test modify price number(38,2);
ERROR at line 1:
ORA-01440: column to be modified must be empty to decrease precision or scale
如上所示,当我们修改字段price的NUMBEr类型的刻度时,
就会遇到ORA-01440: column to be modified must be empty to decrease precision or scale,解决这个问题的方法有两种
方案1:
1:首先对该表做逻辑备份,当然如果你确定没有什么问题,也可以忽略此步骤。
create table test_20170608_bak as select * from test;
2:增加一个临时字段用来复制旧字段数据
alter table test add price_tmp number(38,1);
update test set price_tmp = price;
commit;
select * from test;
3:修改字段price的刻度(Scale)值
update test set price = null;
commit;
select * from test;
alter table test modify price number(38,2);
4:将数据从字段price_tmp更新回price字段
update test set price = price_tmp;
commit;
select * from test;
5:删除临时字段price_tmp
alter table test drop column price_tmp;
select * from test;
方案2:
另外一种方法就是备份数据,然后删除全部数据,然后修改表结构,最后将数据更新回去。如下所示:
1:备份原表数据
create table test_bak as select * from test;
2:清理删除原表数据
truncate table test;
select * from test;
3:修改表资源的精度或标度
alter table test modify price number(38,3);
4:将数据还原回去
insert into test select * from test_bak;
commit;
select * from test;
另外,需要注意的是,这两者方法都必须确保操作时,没有业务或应用程序操作该表,否则会有数据一致性问题。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




