Technical Article

Foreign Key Lookup (Name, columns, tables)

,

This script will find all foreign key contraints in the database it is run on.  Includes.

Name of Foreign Key
Name of table FK is on
Name of column FK is on
Name of table FK references
Name of column FK references

selectso1.Name as FKConstraint, so.Name as FromTable, 
sc.Name as FromColumn, so2.Name as ToTable, sc1.Name as ToColumn
From sysforeignkeys fk (nolock)
JOINsysobjects so (nolock) on so.[id] = fk.fkeyid
JOINsysobjects so1 (nolock) on fk.constid = so1.id
joinsyscolumns sc (nolock) on fk.fkeyid = sc.id and fk.fkey = sc.colid
JOINsysobjects so2 (nolock) on fk.rkeyid = so2.id
joinsyscolumns sc1 (nolock) on fk.rkeyid = sc1.id and fk.rkey = sc1.colid
--whereso.name = <Table Name>

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating