Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Finding Orphan DB Records

Posted on 2014-04-03
4
Medium Priority
?
424 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 668 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 668 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 664 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

916 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