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

--新增主键(没有主键字段,则新增一个主键字段,然后赋值,然后再添加主键)
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 ;

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值