Trace Parent Table from Foreign Key

  • 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!

  • 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

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • This will show you how to get what you need.

    Find Table Reference Levels

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=72957

  • Hi

    Check out the below link

    http://www.dotnetvj.com/2009/03/foreign-key-and-primary-key-in-sql.html

    Thanks -- Vijaya Kadiyala

    http://www.dotnetvj.com

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply