在修改表字段的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. ajax实现文档导出及下载

    做导出一直遇到个问题就是不能用ajax实现一步导出文档,即导出加下载.今天突然想到可以分开来做就上网搜了下,发现一篇比较不错的文章(http://www.cnblogs.com/zj0208/p/59 ...

  2. 网络编程第三讲UDP编写

    网络编程第三讲UDP编写 一丶UDP简介 UDP是面向无连接的.就是说数据传输会丢掉.网络延时比较大的情况下.会早上丢包.例如视频通话.就是UDP UDP不需要建立建立. 下面有UDP编写流程图 下图 ...

  3. JS判断滚动条到底部,页面是否有滚动条

    要判断页面滚动条是否到底,需要了解三个属性: scrollHeight:获取元素内容高度的度量,包括由于溢出导致的视图中不可见内容,说直白点,算上了滚动条不可见的那部分高度. clientHeight ...

  4. spring-boot-2.0.3之quartz集成,数据源问题,源码探究

    前言 开心一刻 着火了,他报警说:119吗,我家发生火灾了. 119问:在哪里? 他说:在我家. 119问:具体点. 他说:在我家的厨房里. 119问:我说你现在的位置. 他说:我趴在桌子底下. 11 ...

  5. 创业公司都在使用的3款Python库

    Instavest上发表了一篇博文,文章分享了深受创业公司喜爱的3款Python库,该文章在Hacker News上引发了开发者的激烈探讨,如果你也对此感兴趣,不妨移步去看下.笔者将该文简译过来以分享 ...

  6. 第一册:lesson fifty nine。

    原文: Is that all? A:I want some envelopes ,please? B:Do you want the large size or small size? A:The ...

  7. 第一册:lesson forty

    原文: Penny's bag. A:Is that bag heavy,Penny? B:Not very. A:Here. Put it on this chair. What's in it? ...

  8. vs 中引用自己创建程序集出现小叹号

    出现的问题: 原因是.net frame work版本不一致 解决方法: 项目单击右键-->属性: 改为与你要引用的项目的程序集的版本一致即可

  9. nginx配置虚拟机

    在/usr/local/nginx/conf目录下nginx.conf文件是nginx的配置文件. 一.通过端口号区分虚拟机 在nginx.conf文件中添加一个Service节点,修改端口号: se ...

  10. Maven(六)Eclipse使用Maven插件创建项目

    1. 创建Maven版Java工程 1.1 具体步骤 1.2 更改默认JDK版本 默认JDK版本过低 可以通过配置setting.xml来更改JDK版本 加入如下代码 <profile> ...