List of all tables with a relationship to a given table or view List of all tables with a relationship to a given table or view sql-server sql-server

List of all tables with a relationship to a given table or view


For SQL Server 2005 and up, use something like:

SELECT    name, OBJECT_NAME(parent_object_id) 'Table'FROM     sys.foreign_keysWHERE     referenced_object_id = OBJECT_ID('Your-referenced-table-name-here')


-- To find all the foreign keys established to a table!-- Columns: FKTABLE_NAME, FKCOLUMN_NAMEsp_fkeys @pktable_name='your table name here'


Say your table name is TableX. If you want to know all foreign key relationships (columns of TableX referenced in other tables and columns of other tables referenced in TableX) you could do this:

select name 'ForeignKeyName',     OBJECT_NAME(referenced_object_id) 'RefrencedTable',    OBJECT_NAME(parent_object_id) 'ParentTable'from sys.foreign_keyswhere referenced_object_id = OBJECT_ID('TableX') or     parent_object_id = OBJECT_ID('TableX')