Listing Foreign Keys of Selected Table in SQL Server
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.
As you can see, the Foreign Keys of the selected table are listed.
Good luck to everyone in business and life.