SQL: List of foreign key relationships

Use the query below 1Please ensure you test any script taken from my website on a test/development machine, before running on a production server. to produce a list of foreign key relationships:

select f.name AS ReferencingTable, fc.name AS ReferencingColumn, 
       r.name AS ReferencedTable, rc.name AS ReferencedColumn
from sysreferences s
inner join sysobjects f on s.fkeyid = f.id
inner join sysobjects r on s.rkeyid = r.id
inner join syscolumns fc on s.fkey1 = fc.colorder and f.id = fc.id
inner join syscolumns rc on s.rkey1 = rc.colorder and r.id = rc.id
order by f.name, r.name

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.