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,下面介绍一下,如何处理这个问题。

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

 

clip_image001[4]

 

 

如上所示,当我们修改字段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> 

 

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

相关文章
|
6月前
|
存储 SQL 关系型数据库
【MySQL异常】Row size too large (> 1982). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DYNA
【MySQL异常】Row size too large (> 1982). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DYNA
63 0
|
4月前
|
关系型数据库 MySQL
MySQL【问题 02】报错 1709 - Index column size too large. The maximum column size is 767 bytes. 可能是最简单的方法
MySQL【问题 02】报错 1709 - Index column size too large. The maximum column size is 767 bytes. 可能是最简单的方法
53 0
|
10月前
|
存储 安全 关系型数据库
Column length too big for column ‘remark‘ (max=65535)解决办法
Column length too big for column ‘remark‘ (max=65535)解决办法
129 0
|
10月前
|
数据库
Data truncation: Incorrect date value: ‘2022-11-28T16:00:00.000Z‘ for column ‘start_date‘ at row 1
Data truncation: Incorrect date value: ‘2022-11-28T16:00:00.000Z‘ for column ‘start_date‘ at row 1
173 0
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“
|
SQL 关系型数据库 MySQL
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT
|
数据库
Data truncation: Out of range value for column ‘estimate_score‘
Data truncation: Out of range value for column ‘estimate_score‘
|
SQL 存储 分布式计算
F1 Query: Declarative Querying at Scale
2013 年的 F1 是基于 Spanner,主要提供 OLTP 服务,而新的 F1 则定位则是大一统:旨在处理 OLTP/OLAP/ETL 等多种不同的 workload。但是这篇新的 F1 论文对 OLTP 的讨论则是少之又少,据八卦是 Spanner 开始原生支持之前 F1 的部分功能,导致 F1 对 OLTP 的领地被吞并了。
F1 Query: Declarative Querying at Scale
|
关系型数据库 MySQL
1293 - Incorrect table definition; there can be only oneTIMESTAMP column with CURRENT_TIMESTAMP
一个表中出现多个timestamp并设置其中一个为current_timestamp的时候经常会遇到 1293 - Incorrect table definition; there can be only oneTIME...
2385 0