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

Published
Categorized as Scripts

By Phil Steffek

Phil Steffek is a professional sumo wrestler who is hoping to represent the U.S. in the next Olympics. During the off season Phil is a Data Architect working on transactional databases, data warehouse implementations, and all aspects of business intelligence. Also, Phil is not really a sumo wrestler. You can get to know a bit more about my non-sumo doings by checking out my LinkedIn Page

Leave a comment

Your email address will not be published. Required fields are marked *