How to Forcefully Alter Table in Oracle

alter-tableoracle

I'm trying to decrease the precision of a column from NUMBER(25) to NUMBER(10) but when I do I got error saying column to be modified must be empty to decrease precision

I do not have any data in the column length more than 10. So I want to change this precision forcefully.

How can I update this forcefully? Is it possible?

Best Answer

As the error message states, you can't just modify the number column to reduce its precision and scale.

Here is one of the workarounds I would use for this situation.

Here is my table.

SQL> desc t1_num;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER(30)

Add another column with the number data type with specified precision.

SQL> alter table t1_num add id2 number(10);

Table altered.

Update the new column with the data from the old column.

SQL> update t1_num set id2=id;

1 row updated.

SQL> commit;

Commit complete.

Set the old column to null to change its specification.

SQL> update t1_num set id=null;

1 row updated.

Modify the column

SQL> alter table t1_num modify id number(10);

Table altered.

Update the column with the original data.

SQL> update t1_num set id=id2;

1 row updated.

SQL> commit;

Commit complete.

Drop the temp column

SQL> alter table  t1_num drop column id2;

Table altered.

SQL> desc t1_num;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER(10)