oracle对没有主键表的新增主键修改表数据操作(没有主键字段,则新增一个主键字段,然后赋值,然后再添加主键)

xiaoxiao2021-02-28  75

--新增主键(没有主键字段,则新增一个主键字段,然后赋值,然后再添加主键) alter table REPORT_ABNORAML add ID number(11) null; --删除序列 --drop SEQUENCE SEQ_REPORT_ABNORAML --增加序列 CREATE SEQUENCE SEQ_REPORT_ABNORAML INCREMENT BY 1 -- 每次加几个 START WITH 1 -- 从1开始计数 NOMAXVALUE -- 不设置最大值 NOCYCLE -- 一直累加,不循环 NOCACHE; --设置默认值 UPDATE REPORT_ABNORAML SET ID=SEQ_REPORT_ABNORAML.nextval; commit; --设置主键属性 alter table REPORT_ABNORAML add primary key (ID); --更新主键字段不为空 alter table REPORT_ABNORAML MODIFY ID number(11) not null; --按条件删除此表中重复的数据 delete from Report_ABNORAML t where t.id in (select id from ( select row_number() over(PARTITION BY t.report_id,t.abnoraml_no order by t.abnoraml_no) as row1,t.id from REPORT_ABNORAML t where 1=1 and report_id in ( select report_id FROM (select ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY report_time desc,report_id desc) as Row1, report_id,customer_id,orgain_id from report_info where 1 = 1 and orgain_id in (1) /*and report_time >= to_char('2015-04-01') and report_time < to_char('2015-09-01') and customer_id in (select customer_id from customer_info where work_unit in ('第三采油厂') */) )TT where TT.Row1 = 1) )a where a.row1!=1); commit; --删除列 alter table REPORT_ABNORAML drop COLUMN ID ;
转载请注明原文地址: https://www.6miu.com/read-34134.html

最新回复(0)