Skip to content
  • There are no suggestions because the search field is empty.

T-SQL Script: List all foreign keys referencing a table

T-SQL Script: List all foreign keys referencing a table

SELECT 
    fk.name AS ForeignKey,
    OBJECT_NAME(fk.parent_object_id) AS ChildTable,
    cpa.name AS ChildColumn,
    OBJECT_NAME(fk.referenced_object_id) AS ParentTable,
    cref.name AS ParentColumn
FROM sys.foreign_keys fk
JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id
JOIN sys.columns cpa ON fkc.parent_object_id = cpa.object_id AND fkc.parent_column_id = cpa.column_id
JOIN sys.columns cref ON fkc.referenced_object_id = cref.object_id AND fkc.referenced_column_id = cref.column_id
WHERE OBJECT_NAME(fk.referenced_object_id) = 'YourTableName';