Solved

Finding Orphan DB Records

Posted on 2014-04-03
4
407 Views
Last Modified: 2014-04-03
From the "Poor DB Design" department:  I have a database with an "Events" table and a "RaceData" table.  Every record in the "RaceData" table should be tied to the "Events" table via the "EventsID" FK-PK relationship.  Here is the problem:  I think I have some orphans in the "RaceData" table.  How can I find and delete them?
0
Comment
Question by:Bob Schneider
4 Comments
 
LVL 5

Accepted Solution

by:
Steve Dubyo earned 167 total points
ID: 39975722
Hi,

Find..
select 
	rc.*
from 
	RaceData rc
left join 
	[Events] e
on
	rc.EventsID = e.EventsID
where
	e.EventsID is null

Open in new window


Delete..
delete
from 
	RaceData rc
left join 
	[Events] e
on
	rc.EventsID = e.EventsID
where
	e.EventsID is null

Open in new window

0
 
LVL 1

Assisted Solution

by:LogisticsOne
LogisticsOne earned 167 total points
ID: 39975727
You can do a
SELECT * FROM RaceData R
WHERE not exists (select * from Events E where E.EventsId = R.EventsID)

This will match all your Events ID's with the RaceData ID's and then compare that to all the ID's in the RaceData table and return values that are in the RaceData table, but no in the Events table.

(or reverse if if you're checking for orphans in the Events table that don't have a match in the RaceData)

I'm not sure about the performance hit, but I use something similar to this on some tables that are around 2million records each and it just takes a few seconds.
0
 
LVL 6

Assisted Solution

by:Dulton
Dulton earned 166 total points
ID: 39975733
I would recommend first looking at what records you're talking about by running this:
SELECT rd.*
  FROM RaceData AS rd
 WHERE NOT EXISTS (SELECT e.EventsID
FROM EVENTS AS e
WHERE rd.EventsID = e.EventsID)

Open in new window


If you see anything other than exactly what you want to delete, DO NOT RUN THE BELOW DELETE.

After you verify what you see returned is exactly what you want deleted, run the delete below:
DELETE FROM RaceData AS rd
WHERE NOT EXISTS 
(SELECT e.EventsID
 FROM Events As e
 WHERE rd.EventsID = e.EventsID)

Open in new window

0
 

Author Closing Comment

by:Bob Schneider
ID: 39975981
Great solutions.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now