Wednesday, March 16, 2011

How Do I Find All The Foreign Keys In A Database

Here is the script which I use always for finding all the Foreign keys in a Database. 

USE AdventureWorks; 
GO
SELECT f.name AS ForeignKey,
OBJECT_NAME (f.parent_object_id) AS TableName,
COL_NAME (fc.parent_object_id,
fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTabName,  -- Reference Column Name
COL_NAME (fc.referenced_object_id,
fc.referenced_column_id) AS ReferenceColName  -- Reference Column Name
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
GO 
How do I find all the foreign keys in a database, Find Tables With Foreign Key Constraint in Database, Find all Primary and Foreign Keys In A Database, In SQL Server 2008, In SQL Server 2008 R2, in sql server 2005, in sql server 2012, in sql server 2000, How to I find all tables that have foreign keys, How to find foreign key dependencies in SQL Server, How can I list all foreign keys referencing a given table in SQL server 2012, TSQL script to find Foreign Key Dependencies on entire database, Finding Foreign Key Constraints, How to find foreign key relationship, How to drop all foreign keys, How to Find the Foreign Keys in a Database, Find Relationship of Foreign Key, What's wrong with foreign keys, How do I see all foreign keys to a table or column.

2 comments:

Post a Comment