Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Checking Table Relational

Author  Topic 

mikneo
Starting Member

1 Post

Posted - 2012-12-07 : 03:41:19
Hi!

Could Anyone Help me how to check a certain data on a table
if that data is related on another table?
Like for instance i have a 10 tables
and i have check one data on a table which has 5 connection related foreign key on the other tables

how to i check it on query sql server 2005/2008 if that data on
table1 is connected to the other tables i have..?

plz help sorry i'm new in database programming..

Mik

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-12-07 : 03:52:02
Which way does the foreign key go?
I assume this is the master row and referenced by the other tables.

select t1.col, count(t2.col), count(t3.col), ...
from t1
left join t2 on t2.col = t1.col
left join t3 on t3.col = t1.col
...
--where t1.col = 'myval'

This will give a 0 for the table if it is not referenced.
Use the where clause if you want to check a specific row.
note: the referencing tables may not have an index on the column so this may table scan.

You can check the individual tables using an inner join rather than left join.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-07 : 08:15:08
do you mean you've not set foreign keys already and need to find out which all table columns a column in a table is related to?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -