SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Checking Table Relational
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mikneo
Starting Member

Philippines
1 Posts

Posted - 12/07/2012 :  03:41:19  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 12/07/2012 :  03:52:02  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
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

India
52317 Posts

Posted - 12/07/2012 :  08:15:08  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.48 seconds. Powered By: Snitz Forums 2000