Sql Server Query to find all foreign keys referring to particular table

Introduction:  In this article I have explained How to get all foreign keys that refers to a particular table in sql server. I.e. listing all tables whose primary key column is used as a foreign key in our table. We can get full details i.e. referenced table with schema, referring table with schema, referenced column, referring column, foreign key constraint name etc


Implementation: Let’s create a table 'tbEmployee' having a primary key column 'EmployeeId' and two columns DeptId and CategoryId as foreign key. DeptId refers to DepartmentId of 'tbDepartment' Table and CategoryId referes to StaffCategoryId column of 'tbStaffCategory' table.


GO
-- Create a table 'tbEmployee' using following script
CREATE TABLE  tbEmployee
(
            EmployeeId          INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
            EmployeeName   VARCHAR(100),
            DeptId                  INT FOREIGN KEY REFERENCES tbDepartment(DepartmentId),
            CategotyId           INT FOREIGN KEY REFERENCES tbStaffCategory(StaffCategoryId),
);


GO
-- Create another table 'tbDepartment' using following script
CREATE TABLE tbDepartment
(                                  
            DepartmentId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
            DepartmentName VARCHAR(100),
);

GO
-- Create another table 'tbStaffCategory' using following script
CREATE TABLE tbStaffCategory
(                                  
            StaffCategoryId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
            StaffCategoryName VARCHAR(100),
);

Query to find all foreign keys referring to particular table

SELECT SCM.name [Schema], OBJECT_NAME(FK.parent_object_id) 'Referenced Table',
COL_NAME(FK.parent_object_id,FKC.parent_column_id) 'Referenced Column' ,
SCM.name [Schema], OBJECT_NAME(FK.referenced_object_id) 'Referring Table',
COL_NAME(FK.referenced_object_id, FKC.referenced_column_id) 'Referring Column',
FK.name 'Foreign Key Name'
FROM sys.foreign_keys FK INNER JOIN sys.schemas SCM on FK.schema_id=SCM.schema_id
INNER JOIN sys.foreign_key_columns AS FKC ON FKC.constraint_object_id = FK.object_id
WHERE FK.parent_object_id = OBJECT_ID('dbo.tbEmployee')

Result:
Schema
Referenced Table
Referenced Column
Schema
Referring Table
Referring Column
Foreign Key Name
dbo
tbEmployee
DeptId
dbo
tbDepartment
DepartmentId
FK__tbEmploye__DeptI__40058253
dbo
tbEmployee
CategotyId
dbo
tbStaffCategory
StaffCategoryId
FK__tbEmploye__Categ__40F9A68C

 Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..