Listing Foreign Keys of Selected Table in SQL Server

Hello everyone,

In this article, I will provide information about listing the Foreign Keys of the selected table in SQL Server.

In SQL Server, in some cases, you may want to list the Foreign Keys of the selected table.

You can easily do this using the code below.

SELECT t.name AS ForeignKeyTable,
       c.name AS ForeignKeyColumn
FROM sys.foreign_key_columns AS fk
    INNER JOIN sys.tables AS t
        ON fk.parent_object_id = t.object_id
    INNER JOIN sys.columns AS c
        ON fk.parent_object_id = c.object_id
           AND fk.parent_column_id = c.column_id
WHERE fk.referenced_object_id =
(
    SELECT object_id FROM sys.tables WHERE name = 'Customers'
)
ORDER BY ForeignKeyTable;

When you run the above query, you will see a result similar to the one below.

Listing Foreign Keys of Selected Table in SQL Server

As you can see, the Foreign Keys of the selected table are listed.

Good luck to everyone in business and life.

179 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!