Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLServer - How to find dependent tables on my table?

Using SQLServer :

I have a table user :

  • id

  • name

  • email

There are some other tables (about 200 more tables), some of which use user.id as foreign key on cascade delete.

So, I want to find out - Which tables use this foreign key (user.id) ?

I am accessing my sql-server with SQL Server Management Studio.

like image 966
Yugal Jindle Avatar asked Oct 04 '11 12:10

Yugal Jindle


People also ask

How do you find table relationships in SQL?

Using SQL Server Management Studio Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose Relationships from the shortcut menu. In the Foreign Key Relationships dialog box, select the relationship with properties you want to view.

How can check parent and child table in SQL Server?

To find out who that child's parent is, you have to look at the column parent_id , find the same ID number in the id column, and look in that row for the parent's name. In other words, Jim Cliffy has no parents in this table; the value in his parent_id column is NULL .

How can I see tables under schema?

The easiest way to find all tables in SQL is to query the INFORMATION_SCHEMA views. You do this by specifying the information schema, then the “tables” view. Here's an example. SELECT table_name, table_schema, table_type FROM information_schema.

What is table dependency in SQL Server?

In a relational database, it isn't just the data that is related, but the database objects themselves. A view, for example, that references tables is dependent upon them, and wherever that view is used the function, procedure or view that uses it depends on it.


2 Answers

The way to get ONLY TABLE references (i.e. tables that uses given table as a foreign key and tables that given table uses the same way) you can use this code snippet:

declare @tableName varchar(64);
set @tableName = 'TABLE';

select
SO_P.name as [parent table]
,SC_P.name as [parent column]
,'is a foreign key of' as [direction]
,SO_R.name as [referenced table]
,SC_R.name as [referenced column]
,*
from sys.foreign_key_columns FKC
inner join sys.objects SO_P on SO_P.object_id = FKC.parent_object_id
inner join sys.columns SC_P on (SC_P.object_id = FKC.parent_object_id) AND (SC_P.column_id = FKC.parent_column_id)
inner join sys.objects SO_R on SO_R.object_id = FKC.referenced_object_id
inner join sys.columns SC_R on (SC_R.object_id = FKC.referenced_object_id) AND (SC_R.column_id = FKC.referenced_column_id)
where
    ((SO_P.name = @tableName) AND (SO_P.type = 'U'))
    OR
    ((SO_R.name = @tableName) AND (SO_R.type = 'U'))
like image 104
Nikita Avatar answered Oct 27 '22 19:10

Nikita


In SQL server management studio, you can right click your table in the object explorer, and then select 'View Dependencies'. This will open a new window in which you can see all other objects (not just tables) that depend on your table, and on which your table depends.

like image 29
Edwin de Koning Avatar answered Oct 27 '22 20:10

Edwin de Koning