oracle快速创建主键

时间:2025-04-15 09:36:31

oracle中,有时我们会发现有一些表中,一些记录它们每个字段的数据 都是一样一样的,即重复数据,这种数据的不存在肯定是不对了。

究其原因,就是该表没有主键,给一个表创建主键,非常容易:

alter table student add constraint pk_student primary key(studentid);

但是如果这表的数据量特别的大,那个创建主键的时间就会特别的长。

下面创建一种,给大表快递创建主键的方法:

CREATE UNIQUE INDEX PK_student ON student(id) nologging parallel 4 ;
alter index PK_student noparallel;
ALTER TABLE student ADD (
CONSTRAINT PK_student
PRIMARY KEY
(id)
USING INDEX PK_student
ENABLE VALIDATE);

当然还是推荐创建表的时候就把主键建好,省事:

create table student (id int primary key not null,name varchar2(20));

再备注一下,删除重复数据的sql,只留有rowid最小的记录

delete from 表 a where (a.Id) in (select Idfrom 表 group by Id having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id having count(*)>1)