Interview Question of the Week #001 – Script to List Foreign Key Relationships and Constraint Name

My friend Vinod Kumar and I wrote SQL Server Interview Questions and Answers, and readers still send me interview questions. I have been saving them, and this weekly series gives short, practical answers. A memorized script may help in an interview, but real experience matters more.

Two brass pegs align with two matching sockets in one joint

Question: Write a script to list foreign key relationships, their column pairs, and the constraint name in the current SQL Server database.

Answer: Join sys.foreign_keys to sys.foreign_key_columns. A composite foreign key has one row for each paired column, so include constraint_column_id to keep its order. Resolve the names from the correct object ID and column ID at each end:

SELECT fk.name AS ConstraintName,
       ps.name AS ReferencingSchema, pt.name AS ReferencingTable,
       pc.name AS ReferencingColumn,
       rs.name AS ReferencedSchema, rt.name AS ReferencedTable,
       rc.name AS ReferencedColumn,
       fkc.constraint_column_id AS PairPosition
FROM sys.foreign_keys AS fk
JOIN sys.foreign_key_columns AS fkc
  ON fkc.constraint_object_id = fk.object_id
JOIN sys.tables AS pt ON pt.object_id = fk.parent_object_id
JOIN sys.schemas AS ps ON ps.schema_id = pt.schema_id
JOIN sys.columns AS pc
  ON pc.object_id = fkc.parent_object_id
 AND pc.column_id = fkc.parent_column_id
JOIN sys.tables AS rt ON rt.object_id = fk.referenced_object_id
JOIN sys.schemas AS rs ON rs.schema_id = rt.schema_id
JOIN sys.columns AS rc
  ON rc.object_id = fkc.referenced_object_id
 AND rc.column_id = fkc.referenced_column_id
ORDER BY ps.name, pt.name, fk.name, fkc.constraint_column_id;

Here, referencing means the table holding the foreign key; referenced means the table whose candidate key it points to. If the key has two columns, expect two result rows for that constraint, in pair order. Run the query in the database you want to inspect. Catalog visibility depends on your permissions.

This diagram shows why the query returns two rows for one composite constraint. Each referencing column maps to its corresponding referenced column:

Diagram mapping two referencing columns to two referenced columns in one foreign key

Shorter versions of this query use OBJECT_NAME and COL_NAME and skip the schema. They work in a small database, but they hide which schema a table belongs to and make composite keys easy to misread. Qualifying schemas and keeping the pair order is the detail I want a candidate to explain, not merely memorize.

Foreign key inventory: Read a composite key right

A composite foreign key is not one row, it is one row per column pair, so keep the pair order.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Constraint and Keys
Previous Post
SQL SERVER – How to Catch Errors While Inserting Values in Table
Next Post
Interview Question of the Week #002 – Script to Find Byte Size of a Row for All the Tables in Database

Related Posts

6 Comments. Leave new

  • Once again a very Good initiative, this is gonna help to every one..thank you Pinal

    Reply
  • As all required fields are available in foreign_keys_columns view except foreign key name, we can use following query also.

    SELECT object_name(constraint_object_id) AS ForeignKey,
    OBJECT_NAME(parent_object_id) AS TableName,
    COL_NAME(parent_object_id,
    parent_column_id) AS ColumnName,
    OBJECT_NAME (referenced_object_id) AS ReferenceTableName,
    COL_NAME(referenced_object_id,
    referenced_column_id) AS ReferenceColumnName
    FROM sys.foreign_key_columns

    Reply
  • This script seems to make no provision for multi-column FKs. There should be an ORDER BY in both queries, but it should be on key order, not on column name.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.