Technical Article

Get All Foreign Key names and Tables referenced

,

This script lists all the foreign keys ,the referenced and the referencing tables , along with the column information.

CREATE VIEW dbo.VwuConstraintAndTableName
AS
---------------------------------------------------
-
-
- BY Vishy
-
-
--------------------------------------------------
select top 100 percent o.name as ForeignKeyName,  object_name(o.parent_obj) as ChildTable, c.name as ReferencingColumnName,object_name(rkeyid) as ParentTable, pc.name as ReferencedColumnName
from dbo.sysforeignkeys 
join sysobjects o  on constid = o.id
join syscolumns c  on c.id = o.parent_obj and c.colid = fkey
join syscolumns pc on pc.id = rkeyid and pc.colid = rkey
order by ChildTable

Rate

3 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

3 (2)

You rated this post out of 5. Change rating