Analyze the relationship among the tables

PandianS
Posted by PandianS under Sql Server category on | Points: 40 | Views : 1780
The script analyze and listout the relationships among the table(s) of the current Database.
USE DatabaseName
Go

DECLARE @TableName VARCHAR(50)

/* If you want to analyze a particular table then, Enter your 'Table name' instead of NULL */
SELECT @TableName =NULL

SELECT OBJECT_NAME(REFERENCED_OBJECT_ID) [Parent Table Name],
COL_NAME(REFERENCED_OBJECT_ID,REFERENCED_COLUMN_ID) [Parent Column Name],
OBJECT_NAME(PARENT_OBJECT_ID) [Child Table Name],
COL_NAME(PARENT_OBJECT_ID,PARENT_COLUMN_ID) [Child Column Name],
OBJECT_NAME(CONSTRAINT_OBJECT_ID) [Constraint Name]
FROM SYS.foreign_key_columns
WHERE REFERENCED_OBJECT_ID = CASE WHEN @TableName IS NULL THEN REFERENCED_OBJECT_ID ELSE OBJECT_ID(@TableName) END
ORDER BY 1,2,3,4

Comments or Responses

Login to post response