SQL: List of foreign key relationships

Use the query below 1 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
  1. Please ensure you test any script taken from my website on a test/development machine, before running on a production server.

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.