Pages

Saturday, June 21, 2014

Find the relational tables

TABLES HAVING RELATION WITH OTHER TABLES IN SQL SERVER


In a good relational database having normal forms being implemented will have relational tables.

This is possible by creating primary and foreign keys between the tables.

Below Query is used to find the tables having primary key and foreign key relations.


 SELECT OBJECT_NAME (referenced_object_id) AS Parent_Table ,  OBJECT_NAME(parent_object_id) AS Child_Table ,  COL_NAME(referenced_object_id,referenced_column_id) AS Parent_Column ,  COL_NAME(parent_object_id,parent_column_id) AS Child_Column ,  OBJECT_NAME(constraint_object_id) AS FK_Name FROM
 sys.foreign_key_columns
 ORDER BY Parent_Table, Child_Table