在修改表字段的NUMBER类型的精度或刻度时,你可能会遇到ORA-01440: column to be modified must be empty to decrease precision or scale,下面介绍一下,如何处理这个问题。测试案例如下:

SQL> drop table test;

 

 

 

Table dropped.

 

 

 

SQL>create table test(product_id  number,  price number(38,1));

 

 

 

Table created.

 

 

 

SQL> insert into test

 

  2  select 1001, 18.2 from dual union all

 

  3  select 1002, 38.5 from dual union all

 

  4  select 1003, 34.8 from dual union all

 

  5  select 1004, 87.4 from dual;

 

 

 

4 rows created.

 

 

 

SQL> commit;

 

 

 

Commit complete.

 

 

 

SQL> alter table test modify price number(38,2);

 

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

 

 

 

 

 

SQL>

如上所示,当我们修改字段price的NUMBEr类型的刻度时,就会遇到ORA-01440: column to be modified must be empty to decrease precision or scale,解决这个问题的方法有两种

方案1:

1:首先对该表做逻辑备份,当然如果你确定没有什么问题,也可以忽略此步骤。作为DBA,一般都应该有强烈的风险意识。

SQL> create table test_20170608_bak

  2  as

  3  select * from test;

 

Table created.

 

SQL> 

2:增加一个临时字段用来复制旧字段数据

SQL> alter table test add price_tmp number(38,1);

 

Table altered.

 

SQL> update test set price_tmp = price;

 

4 rows updated.

 

SQL> commit;

 

Commit complete.

3:修改字段price的刻度(Scale)值

SQL> update test set price = null;

 

4 rows updated.

 

SQL> commit;

 

Commit complete.

 

SQL> alter table test modify price number(38,2); 

 

Table altered.

4:将数据从字段price_tmp更新回price字段

SQL> update test set price = price_tmp;

 

4 rows updated.

 

SQL> commit;

 

Commit complete.

 

SQL> 

5:删除临时字段price_tmp

SQL> alter table test drop column price_tmp;

 

Table altered.

方案2:

另外一种方法就是备份数据,然后删除全部数据,然后修改表结构,最后将数据更新回去。如下所示:

1:备份原表数据

SQL> create table test_bak

  2  as

  3  select * from test;

 

Table created.

 

SQL>

2:清理删除原表数据

SQL> truncate table test;

 

Table truncated.

3:修改表资源的精度或标度

SQL> alter table test modify price number(38,3);

 

Table altered.

 

SQL> 

4:将数据还原回去

SQL> insert into test

  2  select * from test_bak;

 

4 rows created.

 

SQL> commit;

 

Commit complete.

 

SQL> select * from test;

 

PRODUCT_ID      PRICE

---------- ----------

      1001       18.2

      1002       38.5

      1003       34.8

      1004       87.4

 

SQL> 

另外,需要注意的是,这两者方法都必须确保操作时,没有业务或应用程序操作该表,否则会有数据一致性问题。

ORA-01440: column to be modified must be empty to decrease precision or scale的更多相关文章

  1. ORA-01439: column to be modified must be empty to change datatype

    修改数据库字段类型,但是由于数据表已经存在数据,无法修改: 显示错误:  写道 ORA-01439: column to be modified must be empty to change dat ...

  2. Oracle对列的操作总结

    1.更改列名 alter table TABLE_NAME rename column COLUMN_OLD COLUMN_NEW; 2.添加列 alter table TABLE_NAME add ...

  3. Oracle中字段的修改操作语法

      对字段操作 操作方法 更新字段名 alter table TABLE_NAME rename column column_old to column_new; 添加字段 alter table T ...

  4. Oracle中表列由VARCHAR2类型改成CLOB

    情景 原来表中的列定义成VARCHAR2类型,众所周知,VARCHAR2类型最大支持长度为4000.假设因为业务须要.想把此列转换为CLOB类型,在Oracle中直接通过ALTER语句转换是行不通的. ...

  5. oracle数据库中修改已存在数据的字段

    在oracle中,如果已经存在的数据的某些列,假如要更换类型的话,有的时候是比较麻烦的, 会出现:ORA-01439: column to be modified must be empty to c ...

  6. Oracle 如何修改列的数据类型

    链接:http://www.cnblogs.com/david-zhang-index/archive/2012/04/10/2441015.html 对字段操作 操作方法 更新字段名 alter t ...

  7. oracle修改已存在数据的字段类型

    第一次使用oracle数据库,在通过Navicat premium工具修改字段类型时,发现报“ORA-01439: column to be modified must be empty to cha ...

  8. Robot Framework-DatabaseLibrary数据库(MySql)

    Robot Framework-Mac版本安装 Robot Framework-Windows版本安装 Robot Framework-工具简介及入门使用 Robot Framework-Databa ...

  9. 咏南中间件JSON序列类

    咏南中间件JSON序列类 1)支持跨平台.跨语言 2)支持主从表数据序列.还原,支持任意数量的表 主从表数据序列为JSON字符串样式: { "rows": [ { "FD ...

随机推荐

  1. 谷歌浏览器中安装JsonView扩展程序

    实际开发工作中经常用到json数据,那么就会有这样一个需求:在谷歌浏览器中访问URL地址返回的json数据能否按照json格式展现出来. 比如,在谷歌浏览器中访问:http://jsonview.co ...

  2. karma测试实践

    karma是Google团队开发的一套前端测试运行框架,它不同于测试框架(jasmine,mocha等),它运行在这些测试框架之上,主要完成的工作有: 1.karma启动一个web服务器,生成包含js ...

  3. 基于Github&Hexo的个人博客搭建过程

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  4. 腾讯qq发邮件

    <本人新手> 首先要添加引用

  5. 90 行 Python 搭一个音乐搜索工具

    之前一段时间读到了这篇博客,其中描述了作者如何用java实现国外著名音乐搜索工具shazam的基本功能.其中所提到的文章又将我引向了关于shazam的一篇论文及另外一篇博客.读完之后发现其中的原理并不 ...

  6. 【转载】ASP.NET自定义404和500错误页面

    在ASP.NET网站项目实际上线运行的过程中,有时候在运行环境下会出现400错误或者500错误,这些错误默认的页面都不友好,比较简单单调,其实我们可以自行设置这些错误所对应的页面,让这些错误跳转到我们 ...

  7. CheckForIllegalCrossThreadCalls = false 是干嘛的?

    public Form1() {     InitializeComponent();     CheckForIllegalCrossThreadCalls = false; }       在多线 ...

  8. 重构——一个小例子

    菜鸟区域,老鸟绕路! 原代码,这是一个可以借阅影片的小程序,你可以想象成某个大型系统,我想代码应该都能很容易看懂: using System; using System.Collections.Gen ...

  9. [PHP]算法-跳台阶问题的PHP实现

    一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 思路: 1.找规律 f(1)=1 f(2)=2 f(3)=3 f(4)=5 f( ...

  10. spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法

    spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...