A quick T-SQL snippet I dug up from 2009: how to list every foreign key in a SQL Server database, with the tables and columns on both sides of the relationship. The script below still works unchanged on SQL Server 2022 and Azure SQL — the sys.foreign_keys / sys.foreign_key_columns catalog views have been stable since SQL Server 2005.
SELECT
f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName,
OBJECT_NAME(f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
ORDER BY TableName;
(I’ve added the ORDER BY — the original dumped rows in catalog order, which makes the output needlessly hard to scan.)
Notes from 2026
- It’s “constraints”, not “constrains” — the original title had the typo; the query itself was fine.
- Filter to one table with
WHERE OBJECT_NAME(f.parent_object_id) = 'MyTable'when you just need the incoming references for a specific table. - Check enforced status:
f.is_disabledandf.is_not_trustedtell you whether a foreign key is actually being enforced (FKs created withWITH NOCHECKare untrusted and the optimizer will skip them). - Alternative: the graphical equivalent lives in SSMS under the table’s Keys node, but scripting it out like this is the only practical option across a whole database.