Solved

Finding Orphan DB Records

Posted on 2014-04-03
4
405 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great solutions.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

11 Experts available now in Live!

Get 1:1 Help Now