Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list tables in their dependency order (based on foreign keys)?

This question was originally asked by @PrateekGupta


Background

@PrateekGupta wanted to perform bulk insert operation on multiple tables.
The tables have foreign key relationships between themselves.
If an INSERT operation is done on a table with a foreign key before the referenced table is being inserted to, the operation might fail due to violation of the foreign key.

Requirement

Produce a list of tables within a database ordered according to their dependencies.
Tables with no dependencies (no foreign keys) will be 1st.
Tables with dependencies only in the 1st set of tables will be 2nd.
Tables with dependencies only in the 1st or 2nd sets of tables will be 3rd.
and so on...

like image 461
David דודו Markovitz Avatar asked Nov 02 '16 20:11

David דודו Markovitz


People also ask

How do you show dependencies in a table?

Right-click a table, and then click View Dependencies. In the Object Dependencies<object name> dialog box, select either Objects that depend on <object name>, or Objects on which<object name>depends.

What is the foreign key of the order table?

A foreign key column in a table points to a column with unique values in another table (often the primary key column) to create a way of cross-referencing the two tables. If a column is assigned a foreign key, each row of that column must contain a value that exists in the 'foreign' column it references.

How many foreign keys does table order details have?

A table with a foreign key reference to itself is still limited to 253 foreign key references. Greater than 253 foreign key references are not currently available for columnstore indexes, memory-optimized tables, Stretch Database, or partitioned foreign key tables. Stretch Database is deprecated in SQL Server 2022 (16.


2 Answers

The above answers won't work with circular references. You can use this stored procedure instead.

EXEC sp_msdependencies @flags = 8

Can see the flag options here

EXEC sp_msdependencies '?'

Unfortunately this is not available on SQL Azure.

like image 63
Evil Pigeon Avatar answered Oct 22 '22 11:10

Evil Pigeon


    example:

    create table t1 (i int primary key,j int unique)
    create table t2 (i int primary key references t1 (i));
    create table t3 (i int,j int,primary key (i,j));
    create table t4 (i int,j int,  foreign key (i,j) references t3 (i,j));
    create table t5 (i int references t1 (i),j int,foreign key (i,j) references t3 (i,j));
    create table t6 (i int references t2 (i));

with        cte (lvl,object_id,name)
            as 
            (
                select      1
                           ,object_id
                           ,name

                from        sys.tables

                where       type_desc       = 'USER_TABLE'
                        and is_ms_shipped   = 0

                union all

                select      cte.lvl + 1
                           ,t.object_id
                           ,t.name
                from                    cte

                            join        sys.tables  as t

                            on          exists
                                        (
                                            select      null

                                            from        sys.foreign_keys    as fk

                                            where       fk.parent_object_id     = t.object_id 
                                                    and fk.referenced_object_id = cte.object_id
                                        )

                                    and t.object_id <> cte.object_id
                                    and cte.lvl < 30

                where       t.type_desc     = 'USER_TABLE'      
                        and t.is_ms_shipped = 0
            )


select      name
           ,max (lvl)   as dependency_level

from        cte

group by    name

order by    dependency_level
           ,name
;
like image 36
David דודו Markovitz Avatar answered Oct 22 '22 11:10

David דודו Markovitz