Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trace Parent Table from Foreign Key Expand / Collapse
Author
Message
Posted Friday, March 27, 2009 3:55 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, May 16, 2014 9:24 AM
Points: 328, Visits: 395
There is a FK in a table and I'm trying to find out what the values represent. The parent table is not as apparent as i would think it should be. The codes are 1-6 but i have no idea what they stand for.

I would like to learn how to do this because although it is not important now, i can see it being important in the future.

Thanks!
Post #685442
Posted Saturday, March 28, 2009 5:46 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 1,893, Visits: 3,932
I'm not sure if I understood what you are looking for, but if you want to know which fk column of a table references to which other object this should be the solution:
SELECT OBJECT_NAME(fk.referenced_object_id) referenced_object,
OBJECT_NAME(fk.parent_object_id) fk_object,
ref_col.name referenced_column,
fk_col.name fk_column,
fk.name fk_name
FROM sys.foreign_keys fk
JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id
JOIN sys.columns fk_col ON fk.parent_object_id = fk_col.object_id AND fkc.parent_column_id = fk_col.column_id
JOIN sys.columns ref_col ON fk.referenced_object_id = ref_col.object_id AND fkc.referenced_column_id = ref_col.column_id

Greets
Flo



The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Post #685540
Posted Saturday, March 28, 2009 6:11 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:17 AM
Points: 14,204, Visits: 28,531
I don't quite get the question either. If you read the foreign key constraint, it's going to tell you which table is constraining. From there you can either simply select from that table to see what the PK values represent, or better still JOIN against it to retrieve the data.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server Query Performance Tuning
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #685545
Posted Saturday, March 28, 2009 1:59 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:32 PM
Points: 3,110, Visits: 11,529
This will show you how to get what you need.

Find Table Reference Levels
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=72957
Post #685618
Posted Monday, March 30, 2009 12:38 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 25, 2010 6:09 AM
Points: 1,621, Visits: 409
Hi
Check out the below link
http://www.dotnetvj.com/2009/03/foreign-key-and-primary-key-in-sql.html

Thanks -- Vijaya Kadiyala
www.dotnetvj.com


Thanks -- Vijaya Kadiyala
www.dotnetvj.com
SQL Server Articles For Beginers



Post #686386
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse