Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of dependent Tables, SQL Server 2005

Tags:

sql

sql-server

lets say I have table1 = 'foo' and 4 other tables fee1, fee2, fee3, fee4

now say the primary key of foo is a foreign key of fee1 and fee2.

Given the name 'foo' how will I get to know that fee1 and fee2 has foriegn key dependencies on foo.

Please help, a psuedo query would be helpful.

However, I know how to figure out given the name foo how to get the foriegn key dependencies of foo alone. using INFORMATION_SCHEMA.USAGE_KEY_COLUMN.

like image 895
Yash Avatar asked Feb 04 '26 05:02

Yash


1 Answers

Try this in SQL Server Mgmt Studio:

SELECT  
    fk.name,
    OBJECT_NAME(fk.parent_object_id) 'Child table'
FROM 
    sys.foreign_keys fk
WHERE
    fk.referenced_object_id = OBJECT_ID('YourTableNameHEre')

This will list all the foreign key constraints and what table they're coming from that are referencing your YourTableNameHere table.

like image 162
marc_s Avatar answered Feb 05 '26 17:02

marc_s