Removing duplicate rows from table in Oracle

前端 未结 22 1552
醉话见心
醉话见心 2020-11-22 12:57

I\'m testing something in Oracle and populated a table with some sample data, but in the process I accidentally loaded duplicate records, so now I can\'t create a primary ke

22条回答
  •  粉色の甜心
    2020-11-22 13:34

    create table abcd(id number(10),name varchar2(20))
    
    insert into abcd values(1,'abc')
    
    insert into abcd values(2,'pqr')
    
    
    insert into abcd values(3,'xyz')
    
    insert into abcd values(1,'abc')
    
    insert into abcd values(2,'pqr')
    
    insert into abcd values(3,'xyz')
    
    
    select * from abcd
    id  Name
    1   abc
    2   pqr
    3   xyz
    1   abc
    2   pqr
    3   xyz
    
    Delete Duplicate record but keep Distinct Record in table 
    
    DELETE 
    FROM abcd a
    WHERE ROWID > (SELECT MIN(ROWID) FROM abcd b
    WHERE b.id=a.id
    );
    
    run the above query 3 rows delete 
    
    select * from abcd
    
    id  Name 
    1   abc
    2   pqr
    3   xyz
    

提交回复
热议问题