sql - 使用递归查询构建表依赖关系图

标签 sql oracle recursion recursive-query

我正在尝试根据表之间的外键构建表的依赖关系图。该图需要以任意表名作为根开始。我可以,给定一个表名,使用 all_constraints View 查找引用它的表,然后查找引用它们的表,依此类推,但这将是非常低效的。我写了一个递归查询,对所有表执行此操作,但是当我添加时:

START WITH Table_Name=:tablename

它不会返回整个树。

最佳答案

    select parent, child, level from (
select parent_table.table_name parent, child_table.table_name child
 from user_tables      parent_table,
      user_constraints parent_constraint,
      user_constraints child_constraint,
      user_tables      child_table
where parent_table.table_name = parent_constraint.table_name
  and parent_constraint.constraint_type IN( 'P', 'U' )
  and child_constraint.r_constraint_name = parent_constraint.constraint_name
  and child_constraint.constraint_type   = 'R'
  and child_table.table_name = child_constraint.table_name
  and child_table.table_name != parent_table.table_name
)
start with parent = 'DEPT'
connect by prior child = parent

假设一切都在相同的模式中,应该可以工作(当然,替换表名)。如果您需要处理跨模式依赖关系,请使用 DBA_ 版本的数据字典表和 OWNER 和 R_OWNER 列的条件。进一步反射(reflection),这也没有考虑自引用约束(即 MGR 列引用 EMPNO 列的 EMP 表上的约束),因此如果您需要处理这种情况,您必须修改代码来处理这种情况具有自我参照约束。

出于测试目的,我在 SCOTT 模式中添加了一些新表,这些表也引用了 DEPT 表(包括孙子依赖项)
SQL> create table dept_child2 (
  2  deptno number references dept( deptno )
  3  );

Table created.

SQL> create table dept_child3 (
  2    dept_child3_no number primary key,
  3    deptno number references dept( deptno )
  4  );

Table created.

SQL> create table dept_grandchild (
  2    dept_child3_no number references dept_child3( dept_child3_no )
  3  );

Table created.

并验证查询返回了预期的输出
SQL> ed
Wrote file afiedt.buf

  1  select parent, child, level from (
  2  select parent_table.table_name parent, child_table.table_name child
  3   from user_tables      parent_table,
  4        user_constraints parent_constraint,
  5        user_constraints child_constraint,
  6        user_tables      child_table
  7  where parent_table.table_name = parent_constraint.table_name
  8    and parent_constraint.constraint_type IN( 'P', 'U' )
  9    and child_constraint.r_constraint_name = parent_constraint.constraint_name
 10    and child_constraint.constraint_type   = 'R'
 11    and child_table.table_name = child_constraint.table_name
 12    and child_table.table_name != parent_table.table_name
 13  )
 14  start with parent = 'DEPT'
 15* connect by prior child = parent
SQL> /

PARENT                         CHILD                               LEVEL
------------------------------ ------------------------------ ----------
DEPT                           DEPT_CHILD3                             1
DEPT_CHILD3                    DEPT_GRANDCHILD                         2
DEPT                           DEPT_CHILD2                             1
DEPT                           EMP                                     1

关于sql - 使用递归查询构建表依赖关系图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/87877/

相关文章:

sql - 为什么允许我有两个同名索引?

sql - 将存储过程的结果附加到先前执行的存储过程

oracle - 我未使用的 LOB 字段上的 ora 22992

sql-server - 最大连接池大小

javascript - 使用||函数返回值中的运算符

javascript - 递归requestAnimationFrame() javascript循环直接跳到最后

java - Java递归中返回值的用法

c# - SqlBulkCopy 在事务内部插入时阻止任何其他对表的写入

sql - 正则表达式仅用双引号替换第一个分号

php - 'c :/wamp64/bin/php/php7. 0.10/ext/php_oci8_12c.dll' - %1 不是有效的 win32 应用程序