Groups | Blog | Home
all groups > sql server (microsoft) > december 2006 >

sql server (microsoft) : Q: missing parent


G .Net
12/6/2006 11:47:37 AM
Hi

Can anybody tell me the sql query I need to find the following:

I have two tables A and B. A is a parent to B.

I need to find the rows in B which do not have a corresponding foreign key
value to a row in A.

Thanks in advance

G

Ed Murphy
12/6/2006 5:32:34 PM
[quoted text, click to view]

select B.primary_key
from B
left outer join A on B.foreign_key = A.primary_key
where A.primary_key is null

Should this situation be allowed? If not, then (after cleaning up all
existing cases) you should enforce it in the table definitions. (I'll
G .Net
12/6/2006 6:00:10 PM
Thanks Ed

[quoted text, click to view]

Lee Boozer
12/7/2006 10:48:38 PM
Select *
From B
Where Not Exists (Select * From A Where A.keyval = B.keyval)


[quoted text, click to view]
Lee Boozer
12/7/2006 10:53:03 PM
PS - When you get done cleaning up the data, look up "Foreign Key Constraints",
and if at all possible add one to your child table, so your system will never
allow this situation to occur again.


[quoted text, click to view]
AddThis Social Bookmark Button