List Tables with Foreign Key Constraint in a SQL Server Database

Here is a simple query you can use to find tables with Foreign Key Constraint in a SQL Server Database with the names and schemas of the referenced tables:

SELECT fk.name AS Foreign_Key,
SCHEMA_NAME(fk.schema_id) AS Schema_Name,
OBJECT_NAME(fk.parent_object_id) AS Table_Name,
SCHEMA_NAME(o.schema_id) Referenced_Schema_Name,
OBJECT_NAME (fk.referenced_object_id) AS Referenced_Table_Name
FROM sys.foreign_keys fk
INNER JOIN sys.objects o ON fk.referenced_object_id = o.object_id
ORDER BY Table_Name

If the post helped you, please share it:
Pin It

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">