如何检索外键约束数据


10

我正在寻找一个查询,该查询允许检索整个架构的外键信息(每行:引用表和字段,引用表和字段)。

我已经找到了,但是没有提供我需要的所有信息:https : //stackoverflow.com/questions/4389228/sql-for-oracle-to-check-if-a-constraint-exists

我目前正在研究它,可能在接下来的几分钟/几小时内得到解决方案。但是,如果有人已经有了完整的工作解决方案,我将很高兴知道:)


对于SQL Developer中,你可以找到在桌子上的“模型”选项卡中的ERD格式此信息(如参考这里。一个脚本没有什么用处,但如果你只是需要的信息和这里的土地和我一样,也可能是有帮助的。
SnoringFrog

Answers:


13

在打开设计表窗口时(对检索有关外键的信息的查询显示在历史记录窗口中),在对Navicat工具进行的查询进行一些“反向工程”之后,以下是一种解决方案:

SELECT
    CONS.CONSTRAINT_NAME,
    CONS.TABLE_NAME,
    COLS.COLUMN_NAME,
    CONS.R_CONSTRAINT_NAME,
    CONS_R.TABLE_NAME R_TABLE_NAME,
    COLS_R.COLUMN_NAME R_COLUMN_NAME

FROM USER_CONSTRAINTS CONS
    LEFT JOIN USER_CONS_COLUMNS COLS ON COLS.CONSTRAINT_NAME = CONS.CONSTRAINT_NAME
    LEFT JOIN USER_CONSTRAINTS CONS_R ON CONS_R.CONSTRAINT_NAME = CONS.R_CONSTRAINT_NAME
    LEFT JOIN USER_CONS_COLUMNS COLS_R ON COLS_R.CONSTRAINT_NAME = CONS.R_CONSTRAINT_NAME

-- returns only foreign key constraints
WHERE CONS.CONSTRAINT_TYPE = 'R'

ORDER BY CONS.TABLE_NAME, COLS.COLUMN_NAME

2

SQL Developer附带了执行此操作的报告。

它仅针对登录模式执行此操作,但这是一种快速修复方法,可以使其获取数据库中的每个FK-尽管您可能希望省略诸如“ APEX ...”和“ SYS”之类的模式。

它还省略了回收站中的表格之类的内容。

原始报告位于数据字典报告中的“报告”面板中。

这是经过修改的查询,用于获取所有FK。

    SELECT
    c.owner "Owner",
    c.table_name "Table_Name",
    c.constraint_name "Constraint_Name",
    c.delete_rule "Delete_Rule",
    d.columns,
    c.r_owner "Owner of Related Table",
    (
        SELECT
            r.table_name
        FROM
            sys.all_constraints r
        WHERE
            c.r_owner = r.owner
        AND
            c.r_constraint_name = r.constraint_name
    ) "Related Table",
    c.r_constraint_name "Related Constraint"
FROM
    sys.all_constraints c,
    (
        SELECT
            a.owner,
            a.table_name,
            a.constraint_name,
            MAX(
                DECODE(position,1,substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,2,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,3,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,4,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,5,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,6,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,7,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,8,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,9,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,10,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,11,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,12,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,13,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,14,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,15,','
                 ||  substr(column_name,1,30),NULL)
            )
             ||  MAX(
                DECODE(position,16,','
                 ||  substr(column_name,1,30),NULL)
            ) columns
        FROM
            sys.all_constraints a,
            sys.all_cons_columns b
        WHERE
            a.constraint_name = b.constraint_name
        AND
            a.owner = b.owner
        AND
            a.constraint_type = 'R'
        AND
            substr(a.table_name,1,4) != 'BIN$'
        AND
            substr(a.table_name,1,3) != 'DR$'
        AND (
                :table_name IS NULL
            OR
                instr(upper(a.table_name),upper(:table_name) ) > 0
        ) GROUP BY
            a.owner,
            a.table_name,
            a.constraint_name
    ) d
WHERE
    c.owner = d.owner
AND
    c.table_name = d.table_name
AND
    c.constraint_name = d.constraint_name
ORDER BY
    c.owner,
    c.table_name,
    c.constraint_name

这就是该报告的样子。

在此处输入图片说明


1

一点点复杂的代码也会在cols上转储注释(基于Frosty代码):

SELECT
    dt.table_name, dt.column_name, dt.data_type, dt.data_length,
    constr.r_tbl r_table, constr.r_col r_column,
    comm.comments
  FROM user_col_comments comm, user_tab_columns dt
  LEFT OUTER JOIN (
    SELECT
      cons.table_name tbl,
      cols.column_name col,
      cons_r.table_name r_tbl,
      cols_r.column_name r_col
    FROM user_constraints cons
      LEFT JOIN user_cons_columns cols ON cols.constraint_name = cons.constraint_name
      LEFT JOIN user_constraints cons_r ON cons_r.constraint_name = cons.r_constraint_name
      LEFT JOIN user_cons_columns cols_r ON cols_r.constraint_name = cons.r_constraint_name
    WHERE cons.constraint_type = 'R'
    ) constr ON constr.tbl = dt.table_name AND constr.col = dt.column_name
  WHERE dt.table_name = comm.table_name
    AND dt.column_name = comm.column_name
  ORDER BY dt.table_name, dt.column_name
  ;

为了使输出更具可读性,我使用break on TABLE_NAME;sqlplus(请看我的问题/programming/14998296/print-only-first-unique-value-for-column-that-order-by-in-oracle-sqlplus /)。

UPDATE更简单的查询,该查询收集对给定表具有FK引用的表的列表(如果要在表重命名后清除约束,则很有用):

select * from SYS.USER_CONSTRAINTS cons
  join SYS.USER_CONSTRAINTS rcons on rcons.CONSTRAINT_NAME = cons.R_CONSTRAINT_NAME
  where cons.CONSTRAINT_TYPE = 'R' and rcons.TABLE_NAME 'TBL_NAME';

select * from SYS.USER_CONSTRAINTS cons
  join SYS.USER_CONSTRAINTS rcons on rcons.CONSTRAINT_NAME = cons.R_CONSTRAINT_NAME
  where cons.CONSTRAINT_TYPE = 'R' and rcons.TABLE_NAME like '%/_OLD' escape '/';
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.