Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding Orphan DB Records

Posted on 2014-04-03
4
Medium Priority
?
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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