ORA-01440: column to be modified must be empty to decrease precision or scale

在修改表字段的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> 

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

时间: 2024-08-24 04:37:43

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

ORA_ERROR大全

转自:http://blog.csdn.net/haiross/article/details/12839229 常见错误:-60 ORA00060: deadlock detected while waiting for resource 一般错误:  - 1 ORA00001: unique constraint (.) violated  -17 ORA00017: session requested to set trace event  -18 ORA00018: maximum nu

Oracle中表列由VARCHAR2类型改成CLOB

情景 原来表中的列定义成VARCHAR2类型,众所周知,VARCHAR2类型最大支持长度为4000.假设因为业务须要.想把此列转换为CLOB类型,在Oracle中直接通过ALTER语句转换是行不通的.以下依据详细事例解说在Oracle数据库中怎样把表列由VARCHAR2类型转换为CLOB类型. 演示样例准备 1. 新建两张张表TB_WITHOUT_DATA(此VARCHAR2列不包括数据)和TB_WITH_DATA(此Varchar2列包括数据) create table TB_WITHOUT_

Oracle中修改已存在数据的字段类型

原字段类型为varchar2,实际上均为数字,想将字段类型改为number,报错ORA-01439: column to be modified must be empty to change datatype 解决方法: tablename为表名称,colname为要修改的字段名 alter table tablename add tempcolumn varchar2(100);--添加临时字段tempcolumn update tablename set tempcolumn=colnam

oracle表和对象基础维护笔记

一 oracle表和对象基础维护笔记 1.1 常见概念 1.2 创建表 1.3 表常见字段 1.4 增加或删除字段 1.5 更新字段 1.6 重命名表 1.7 改变表存储表空间和存储参数 1.8 删除表 1.9 表注释 1.10 分区表的管理 1.11 常用数据字典 二 约束 2.1 非空约束 2.2 主键约束 2.3 唯一性约束 2.4 外键约束 2.5 约束管理 三 索引 3.2 创建索引 3.3 改变索引存储参数 3.4 重建索引 3.5 索引碎片整理 3.6 删除索引 3.7 数据字典

sql server query to get the list of column name in a table

--SQL Server 2005, 2008 or 2012: SELECT * FROM information_schema.tables --SQL Server 2000: SELECT * FROM sysobjects WHERE xtype='U' SELECT * FROM sysobjects WHERE xtype='U' SELECT TABLE_NAME FROM geovidnu.INFORMATION_SCHEMA.Tables SELECT sobjects.na

ActiveRecord::ConnectionAdapters::TableDefinition | column相关方法学习笔记

TableDefinition 的对象,就是我们在写 migrate 的时候使用的对象 t 如下 class SomeMigration < ActiveRecord::Migration def up create_table :foo do |t| puts t.class # => "ActiveRecord::ConnectionAdapters::TableDefinition" end end def down ... end end column 方法语法是:

ActiveRecord::ConnectionAdapters::SchemaStatements | 有关 Column 的常见方法笔记

跟 column相关的常见的方法有: column_exists? (table_name, column_name, type = nil, options = {}) add_column (table_name, column_name, type, options = {}) remove_column (table_name, column_name, type=nil, options = {}) remove_columns (table_name, *column_name) c

Hibernate JPA @Column说明

jpa @Column说明 @Column(name="columnName";boolean unique() default false;boolean nullable() default true; boolean insertable() default true;boolean updatable() default true; String columnDefinition() default ""; String table() default &q

Spring JPA注解和Column属性 详解

该注解的定义如下: @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface Column { String name() default ""; boolean unique() default false; boolean nullable() default true; boolean insertable() default true; boolean updatable() default true; Str