Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> RE: Duplicate rows

RE: Duplicate rows

From: Rachel Carmichael <wisernet100_at_yahoo.com>
Date: Wed, 2 Jun 2004 08:26:31 -0700 (PDT)
Message-ID: <20040602152631.86465.qmail@web60710.mail.yahoo.com>


depends on if you want to remove ALL rows that are duplicates or just remove the second, third etc...

since I JUST did this as part of a conversion from one app to another, it's slightly more involved but not really too hard.

  1. create exception table and unique constraint, try to enable constraint with exceptions into exception table. This gets BOTH sides of the duplication into the exception table.
  2. build a small table of columns that are unique plus the rowid from the source table, selecting rowids from the exceptions table
  3. delete the duplicates in the smaller table (runs a LOT faster than trying to delete the duplicates from the source)
  4. delete the rows in the source table where the rowid is in the smaller table
  5. reenable unique constraint to verify duplicates gone and ensure they don't come back
  6. figure out how the heck you got duplicates in in the first place!

Sample code below:

NOTE: when I do this, I do rowcounts of all tables in between each DML statement, just to verify that I'm deleting the right number of rows

  1. alter table source_table add constraint ux_constraint UNIQUE (<unique_columns list>) exceptions into exceptions /
  2. drop table dupes /

create table dupes as
select <unique_columns list>, rowid myrowid from source_table where rowid in (select row_id from exceptions) /

truncate table exceptions
/

3) delete from dupes a where rowid > (select min(rowid) from dupes b
where a.unique_column1=b.unique_column1
and a.unique_column2=b.unique_column2 .... repeat above for all columns
)
/

4) delete from source_table s where s.rowid in (select myrowid from dupes);

5) alter table source_table add constraint ux_constraint UNIQUE (<unique_columns list>) exceptions into exceptions /

6) that one YOU have to figure out.... in my case, the original app did not have constraints on it. Bless those 3rd party apps written for "generic" databases!

Rachel


Do you Yahoo!?
Friends. Fun. Try the all-new Yahoo! Messenger. http://messenger.yahoo.com/

Please see the official ORACLE-L FAQ: http://www.orafaq.com

To unsubscribe send email to: oracle-l-request_at_freelists.org put 'unsubscribe' in the subject line.
--
Archives are at http://www.freelists.org/archives/oracle-l/
FAQ is at http://www.freelists.org/help/fom-serve/cache/1.html
-----------------------------------------------------------------
Received on Wed Jun 02 2004 - 10:23:50 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US