(Transfer) Oracle Modify Field Type Method Summary

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.
 
 将数字类型改为字符串类型(本来有值了):
/*修改原字段名name为name_tmp*/
alter table t_menu_type rename column T_TYPE_ID to T_TYPE_ID_tmp;

/*增加一个和原字段名同名的字段name*/
alter table t_menu_type add T_TYPE_ID varchar2(40);

/*将原字段name_tmp数据更新到增加的字段name*/
update t_menu_type set T_TYPE_ID=trim(T_TYPE_ID_tmp);

/*更新完,删除原字段name_tmp*/
alter table t_menu_type drop column T_TYPE_ID_tmp;
 
REFS: http://blog.csdn.net/gdjlc/article/details/23762549/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326443161&siteId=291194637