Please disable your adblock and script blockers to view this page
 

Sql Server Query to Find All Foreign Keys References of Particular Table

Introduction:  In this article I have explained How to get all foreign key reference of any particular table with full details i.e. referenced table with schema, referring table with schema, referenced column, referring column, foreign key constraint name in sql server. I.e. listing all child tables containing primary key column of parent table as a foreign key.


Implementation: Lets create a parent table 'tbEmployee' having Primary Key column 'EmployeeId'  and two child tables 'tbEmployeeExperience' and 'tbEmployeeQualification' having EmployeeId as foreign key as.

 -- Create a parent table using following script
CREATE TABLE tbEmployee
(
            EmployeeId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
            EmployeeName VARCHAR(100)
);

GO
-- Create a child table that refer above parent table 'tbEmployee' using following script
CREATE TABLE tbEmployeeExperience
(
            CompanyName VARCHAR(200),
            TotalMonths INT,
            EmployeeId INT FOREIGN KEY REFERENCES tbEmployee(EmployeeId),
);

GO
-- Create another child table that refer above parent table 'tbEmployee' using following script
CREATE TABLE tbEmployeQualification
(
            QualificationName VARCHAR(200),
            EmployeeId INT FOREIGN KEY REFERENCES tbEmployee(EmployeeId),
);


Query to find all foreign keys reference of particular table

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

Result:
Schema
Referenced Table
Referenced Column
Schema
Referring Table
Referring Column
Foreign Key Name
dbo
tbEmployee
EmployeeId
dbo
tbEmployeeExperience
EmployeeId
FK__tbEmploye__Emplo__00750D23
dbo
tbEmployee
EmployeeId
dbo
tbEmployeQualification
EmployeeId
FK__tbEmploye__Emplo__025D5595

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.. ConversionConversion EmoticonEmoticon