如何按依赖顺序列出表(基于外键)?

时间:2016-11-02 20:10:26

标签: sql-server foreign-keys database-metadata

这个问题最初是由@PrateekGupta提出的

背景

@PrateekGupta想要对多个表执行批量插入操作 表格之间有外键关系 如果在插入引用的表之前对具有外键的表执行INSERT操作,则操作可能会因违反外键而失败。

Reuierment

根据数据库的依赖关系生成数据库中的表列表 没有依赖关系的表(没有外键)将是第1个 仅在第1组表中具有依赖关系的表将是第2个 仅在第1或第2组表中具有依赖关系的表将是第3个 等等...

5 个答案:

答案 0 :(得分:13)

    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
;

答案 1 :(得分:2)

感谢大卫。

如果有人需要,我刚刚将架构名称添加到他的查询中

GetXML

答案 2 :(得分:1)

脚本的Oracle版本:

with 
    foreign_keys as (
        select
          src_cc.owner as src_owner,
          src_cc.table_name as src_table,
          src_cc.column_name as src_column,
          dest_cc.owner as dest_owner,
          dest_cc.table_name as dest_table,
          dest_cc.column_name as dest_column,
          c.constraint_name
        from
          all_constraints c
        inner join all_cons_columns dest_cc on
          c.r_constraint_name = dest_cc.constraint_name
          and c.r_owner = dest_cc.owner
        inner join all_cons_columns src_cc on
          c.constraint_name = src_cc.constraint_name
          and c.owner = src_cc.owner
        where
          c.constraint_type = 'R'
          and dest_cc.owner = :owner),
    cte (lvl,table_name) as (
        select 1, table_name
        from all_tables
        where 
            owner = :owner
        union all
        select cte.lvl + 1, t.table_name
        from cte
        join all_tables t on exists (
            select null 
            from foreign_keys fk 
            where 
                fk.src_table = t.table_name
                and fk.dest_table = cte.table_name
        )
        and t.table_name <> cte.table_name
            AND cte.lvl < 30
    )
select table_name, max (lvl)   as dependency_level
from        cte
group by    table_name
order by    dependency_level desc, table_name
;

答案 3 :(得分:1)

以上答案不适用于循环引用。您可以改用此存储过程。

EXEC sp_msdependencies @flags = 8

可以在此处查看标志选项

EXEC sp_msdependencies '?'

不幸的是,这在SQL Azure上不可用。

答案 4 :(得分:0)

我需要自己做这件事,希望有人已经为Postgres做过,但是什么也没找到,所以我就把它留在这里:

WITH RECURSIVE t AS (
    SELECT relnamespace as nsp, oid as tbl, null::regclass as source, 1 as level
    FROM pg_class
    WHERE relkind = 'r'
        AND relnamespace not in ('pg_catalog'::regnamespace, 'information_schema'::regnamespace)
UNION ALL
    SELECT c.connamespace as nsp, c.conrelid as tbl, c.confrelid as source, p.level + 1
    FROM pg_constraint c
    INNER JOIN t p ON (c.confrelid = p.tbl AND c.connamespace = p.nsp)
    WHERE c.contype = 'f'
        AND c.connamespace not in ('pg_catalog'::regnamespace, 'information_schema'::regnamespace)
)
SELECT nsp::regnamespace, tbl::regclass
FROM t
GROUP BY nsp, tbl
ORDER BY max(level) DESC;

这是两个用例之间的混合查询。如果需要查看外键引用到哪个表,则可以改为删除GROUP BYSELECT source::regclass