关于 ORA-01439: 要更改数据类型, 则要修改的列必须为空
Posted yxshao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于 ORA-01439: 要更改数据类型, 则要修改的列必须为空相关的知识,希望对你有一定的参考价值。
需求:原本是定义5个字段存储的类型全部都是 数字类型,后来需求变了,每个字段可能存储粘接类型 如:135/200这种格式
原本的数据表结构:
CREATE TABLE MTG_SOC_MEASURE(
PKEY NUMBER(10,0),
MTG_EQUIP_INFO_PTR NUMBER(10,0), --机器信息,指向MTG_EQUIP_INFO
CUSTOMER_PART_NUMBER VARCHAR2(35), --生产型号,指向DATA0050
WORK_ORDER_NUMBER VARCHAR2(50),--工单号
FACE_TIME VARCHAR(10), --面次 TOP BOT
TTYPE VARCHAR2(30), --类别:1代表“首板”,2代表“过程监控”
SIZE_REQUIREMENTS VARCHAR(10),--尺寸要求
MEASURE_1 NUMBER(10,3),
MEASURE_2 NUMBER(10,3),
MEASURE_3 NUMBER(10,3),
MEASURE_4 NUMBER(10,3),
MEASURE_5 NUMBER(10,3),
}
现在需要把 MEASURE_1、 MEASURE_2、 MEASURE_3、 MEASURE_4、 MEASURE_5数据类型改为VARCHAR2(50),但是表已经存在记录,因此需要中间字段进行操作,才能实现,分为以下5个步骤,如下:
--第一步,在表中加一个临时字段
alter table MTG_SOC_MEASURE add MEASURE_1_T NUMBER(10,3);
alter table MTG_SOC_MEASURE add MEASURE_2_T NUMBER(10,3);
alter table MTG_SOC_MEASURE add MEASURE_3_T NUMBER(10,3);
alter table MTG_SOC_MEASURE add MEASURE_4_T NUMBER(10,3);
alter table MTG_SOC_MEASURE add MEASURE_5_T NUMBER(10,3);
--第二步,将目标字段的值付给临时字段,并将目标字段置空
update MTG_SOC_MEASURE set MEASURE_1_T=MEASURE_1,MEASURE_1=null;
update MTG_SOC_MEASURE set MEASURE_2_T=MEASURE_2,MEASURE_2=null;
update MTG_SOC_MEASURE set MEASURE_3_T=MEASURE_3,MEASURE_3=null;
update MTG_SOC_MEASURE set MEASURE_4_T=MEASURE_4,MEASURE_4=null;
update MTG_SOC_MEASURE set MEASURE_5_T=MEASURE_5,MEASURE_5=null;
--第三步,修改目标类型
alter table MTG_SOC_MEASURE modify MEASURE_1 varchar2(50);
alter table MTG_SOC_MEASURE modify MEASURE_2 varchar2(50);
alter table MTG_SOC_MEASURE modify MEASURE_3 varchar2(50);
alter table MTG_SOC_MEASURE modify MEASURE_4 varchar2(50);
alter table MTG_SOC_MEASURE modify MEASURE_5 varchar2(50);
--第四步,将临时字段的值付给目标字段,并将临时字段置空
update MTG_SOC_MEASURE set MEASURE_1=MEASURE_1_T,MEASURE_1_T=null;
update MTG_SOC_MEASURE set MEASURE_2=MEASURE_2_T,MEASURE_2_T=null;
update MTG_SOC_MEASURE set MEASURE_3=MEASURE_3_T,MEASURE_3_T=null;
update MTG_SOC_MEASURE set MEASURE_4=MEASURE_4_T,MEASURE_4_T=null;
update MTG_SOC_MEASURE set MEASURE_5=MEASURE_5_T,MEASURE_5_T=null;
--第五步,最后一步,删除临时字段
alter table MTG_SOC_MEASURE drop column MEASURE_1_T;
alter table MTG_SOC_MEASURE drop column MEASURE_2_T;
alter table MTG_SOC_MEASURE drop column MEASURE_3_T;
alter table MTG_SOC_MEASURE drop column MEASURE_4_T;
alter table MTG_SOC_MEASURE drop column MEASURE_5_T;
修改后的表结构如下;
PKEY NOT NULL NUMBER(10)
MTG_EQUIP_INFO_PTR NUMBER(10)
CUSTOMER_PART_NUMBER VARCHAR2(35)
WORK_ORDER_NUMBER VARCHAR2(50)
FACE_TIME VARCHAR2(10)
TTYPE VARCHAR2(30)
SIZE_REQUIREMENTS VARCHAR2(50)
MEASURE_1 VARCHAR2(50)
MEASURE_2 VARCHAR2(50)
MEASURE_3 VARCHAR2(50)
MEASURE_4 VARCHAR2(50)
MEASURE_5 VARCHAR2(50)
以上是关于关于 ORA-01439: 要更改数据类型, 则要修改的列必须为空的主要内容,如果未能解决你的问题,请参考以下文章