Summary of Oracle Modify Field Type Methods

Reprinted from: http://blog.csdn.net/gdjlc/article/details/23762549/

There is a table named tb, the field segment named name, and the data type nchar(20).


1. Assuming that the field data is empty, no matter what the field type is, you can directly execute:
alter table tb modify (name nvarchar2(20));

2. Assuming that the field has data, change to nvarchar2(20) and execute directly:
alter table tb modify (name nvarchar2(20));

3. Assuming there is data in the field, when it is changed to varchar2(40), it will pop up: "ORA-01439: To change the data type, the column to be modified must be empty", At this time, the following methods should be used to solve this problem :

/*Modify the original field name name to name_tmp*/
alter table tb rename column name to name_tmp;

/*Add a field name with the same name as the original field name*/
alter table tb add name varchar2(40);

/*Update the original field name_tmp data to the added field name*/
update tb set name=trim(name_tmp);

/*After updating, delete the original field name_tmp*/
alter table tb drop column name_tmp;

Summary:
1. When the field has no data or the new type to be modified is compatible with the original type, you can modify it directly.
2. When the field has data and the new type to be modified is incompatible with the original type, it is necessary to indirectly create a new field to transfer.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326565046&siteId=291194637