在修改表字段的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. MyBatis源码解析(六)——DataSource数据源模块之池型数据源

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6675674.html 1 回顾 上一文中解读了MyBatis中非池型数据源的源码,非池型也 ...

  2. Git+Gitlab+Ansible的roles实现一键部署Nginx静态网站(一)--技术流ken

    前言 截止目前已经写了<Ansible基础认识及安装使用详解(一)--技术流ken>,<Ansible常用模块介绍及使用(二)--技术流ken><Ansible剧本介绍及 ...

  3. CSS盒模型及边距问题

    盒模型是CSS的基石之一,页面的每一个元素都被看作一个矩形框,分别由外边距,边框,内边距,内容组成, 在CSS中,width和height的值指的是内容的宽高,增加外边距,边框,内边距并不会对内容造成 ...

  4. [转] JSON Web Token in ASP.NET Web API 2 using Owin

    本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...

  5. [转]js 取得 Unix时间戳(Unix timestamp)

    本文转自:https://blog.csdn.net/o0snow/article/details/6858829 js 取得 Unix时间戳 Unix时间戳(Unix timestamp),或称Un ...

  6. 【转载】阿里云ECS服务器监控资源使用情况

    在阿里云Ecs服务器运维过程中,无论是Centos系统还是Windows系统,有时候我们需要监控分析最新的服务器资源利用率等运行情况,例如最近3个小时CPU使用率情况.内存使用率.网络流入带宽.网络流 ...

  7. C# Winform开发以及控件开发的需要注意的,被人问怕了,都是基础常识

    我是搞控件开发的,经常被人问,所以把一些问题记录了下来!如果有人再问,直接把地址丢给他看. 一. 经常会有人抱怨Winform界面闪烁,下面有几个方法可以尽可能的避免出现闪烁 1.控件的使用尽量以纯色 ...

  8. .NET 单元测试的利剑——模拟框架Moq(简述篇)

    .NET 单元测试的利剑--模拟框架Moq 前言 这篇文章是翻译文,因为通过自己参与的项目,越发觉得单元测试的重要性,特别是当跟业务数据打交道的时候的,Moq就如雪中送炭,所以想学习这个框架,就从这篇 ...

  9. [Linux] 简单安装和使用composer

    wget https://getcomposer.org/installer //下载一个脚本文件 php installer //php执行下这个php脚本 mv composer.phar /us ...

  10. 【Java每日一题】20170117

    20170116问题解析请点击今日问题下方的“[Java每日一题]20170117”查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; import jav ...