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
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.

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



Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 2,087, 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, referenced_column, fk_column, 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


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



Group: General Forum Members
Last Login: Today @ 3:34 PM
Points: 15,661, Visits: 28,051
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
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 @ 1:10 PM
Points: 3,135, Visits: 11,480
This will show you how to get what you need.

Find Table Reference Levels
Post #685618
Posted Monday, March 30, 2009 12:38 PM



Group: General Forum Members
Last Login: Monday, October 25, 2010 6:09 AM
Points: 1,621, Visits: 409
Check out the below link

Thanks -- Vijaya Kadiyala

Thanks -- Vijaya Kadiyala
SQL Server Articles For Beginers

Post #686386
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse