Pages

Thursday, December 20, 2012

Deleting duplicate data in the database

This post if for db beginners who have created duplicate records in their db by mistake.
Let your scripts be like this.
--------------------------------------------------------------------------------------
-----------------Table creation-------------------------------------------------------

 CREATE TABLE CALENDAR
   ( "ROW_ID" NUMBER,
"DAY" NUMBER,
"MONTH" NUMBER,
"YEAR" NUMBER
   ) ;

--------------------------------------------------------------------------------------
-----------------Populating data------------------------------------------------------

Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2000,8,9,2011);
Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2001,4,1,2012);
Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2002,6,4,2012);
Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2003,7,6,2012);
Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2004,5,9,2012);

Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2000,8,9,2011);
Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2001,4,1,2012);
Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2002,6,4,2012);
Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2003,7,6,2012);
Insert into CALENDAR (ROW_ID,DAY,MONTH,YEAR) values (2004,5,9,2012);
--------------------------------------------------------------------------------------
Now you have two sets of redundant data.
Here, what we want to do is to remove the duplicate data that we inserted. To remove that run the following piece of script.

--------------------------------------------------------------------------------------
-----------------Removing duplicate  data--------------------------------------------



DELETE FROM CALENDAR
      WHERE ROWID NOT IN (SELECT   MIN (ROWID)
                              FROM CALENDAR
                          GROUP BY ROW_ID,DAY,MONTH,YEAR);
--------------------------------------------------------------------------------------


Now you can see that all the duplicate records have gone.




No comments:

Post a Comment