生成表的唯一ID

时间:2018-07-19 13:18:08

标签: sql oracle

假设我有示例表,但是我误将了 PK 删除了。我想重新生成唯一的ID号,并更新现有(已修改的)表

4 个答案:

答案 0 :(得分:1)

您为内部sql尝试过density_rank吗?

select dense_rank() over (partition by dname order by dname)
     , dname 
from   department
group by dname

答案 1 :(得分:1)

如果您的数据不太大:

[[9, 16, 21], [24, 25, 24], [21, 16, 9]]

答案 2 :(得分:0)

也许一个简单的临时序列就可以了:

create sequence my_temp_seq1;

update department set cat_id = my_temp_seq1.nextval;

drop sequence my_temp_seq1;

答案 3 :(得分:0)

CREATE SEQUENCE my_seq start with 1 increment by 1;

CREATE OR REPLACE PROCEDURE fixDept
  IS 
    v_next number;
BEGIN
    FOR depts in (SELECT DISTINCT DNAME FROM department)
    LOOP
    v_next := my_seq.nextval;
      UPDATE department  
        SET CAT_ID = v_next 
        WHERE DNAME = depts.DNAME;
    END LOOP;
END fixDept;


EXECUTE fixDept;