Finding SQL Server Foreign Keys – The fast way


There are a dozen different ways to find the foreign keys in an MS SQL database.  You can make it as hard on yourself as you want or as time consuming as your boss will allow.  SQL Server consultants turn to the following T-SQL script to get a list of SQL Server Foreign Keys as quickly and efficiently as possible:

SELECT
OBJECT_SCHEMA_NAME(parent_object_id) ParentSchema
,OBJECT_NAME(parent_object_id) ParentTable
,OBJECT_SCHEMA_NAME(referenced_object_id) ChildSchema
,OBJECT_NAME(referenced_object_id) ChildTable
,name FKName
,object_id
,is_disabled
,is_not_for_replication
,is_not_trusted
,delete_referential_action
,update_referential_action
FROM
sys.foreign_keys
ORDER BY
1
,2
,3
,4


Leave a Reply

Your email address will not be published.