Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete duplicate records

Status
Not open for further replies.

KingSlick

Programmer
Mar 9, 2007
45
US
I have a table that contains over 100k records. The problem that I have is there are about 5k records that are duplicates. All of these records are duplicated twice and the only field that is the same is the primary key. Is there any query that I could use that will only delete one of each of the duplicated records.

Thanks in advance.

-SM
 
Code:
the only field that is the same is the primary key
So they are duplicate primary keys not duplicate records.
How will you know which one of the duplicated records is the correct one to delete?


Keith
 
Ooops. Sorry. What I meant was the primary key is the only field that is different.

Either of the duplicated records can be deleted as long as there is only 1 remaining.

Thanks
 
I have an update script that adds them to the table. There was a problem with the script and had to be restarted. This caused the duplicates to be added again. So they are semi-scattered. An example is below. As you can see, the id is the primary key.

Code:
____________
|id | code |
------------
|123 | 987 |
|124 | 988 |
|125 | 989 |
|126 | 990 |
|127 | 987 |
|128 | 988 |
|129 | 989 |
|130 | 990 |
 
Yes all the records will have a unique code field. I have corrected the problem. I just deleted everything with an id greater than that of where the problem started. But thanks for all of the help anyways.

SM
 
You may also want to make that field UNIQUE. You could always add an ON DUPLICATE KEY UPDATE... to your INSERT statement(s).

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top