【问题标题】:Joining oracle metadata tables加入 oracle 元数据表
【发布时间】:2020-02-25 14:20:00
【问题描述】:

我正在尝试加入元数据表以查询表的统计信息,例如触发器计数、序列、包、过程、视图等,但我找不到加入表的键。这是我的查询

SELECT * 
FROM user_constraints   a
    JOIN user_tables    b ON a.table_name = b.table_name
    JOIN user_triggers  c ON a.table_name = c.table_name;

我想做一个大查询以从所有主要元数据表中提取所有数据 谢谢

【问题讨论】:

  • 例如,“桌子上的包裹数”是什么意思?在开始寻找编码解决方案之前,问题必须在非计算方面有意义。
  • 上面的查询有什么问题,在我看来它应该可以工作。有错误吗?这怎么不满足您的需求?
  • 我认为您丢失的数据(序列、包等)存储在 user_dependencies 表中...
  • 你没有做任何计数,所以你可能想对各个表进行一些分组?
  • 这样的查询将遭受“粉丝陷阱”(stackoverflow.com/a/16425001/5174436)。例如,表 X 具有三个约束和四个触发器——您将在表 X 的结果集中获得 12 行,这可能不是您想要的。问问自己这个问题:“我希望结果集中的每一行都代表一个......什么?”。您可能正在寻找多个小查询(可能通过UNION ALL 连接)而不是“一个大查询”。

标签: sql oracle oracle11g metadata oracle-sqldeveloper


【解决方案1】:

这是一个开始,显示约束、索引和触发器...

SELECT   t.owner,
         t.table_name,
         -- Constraints
        ( SELECT count(*) 
          FROM   all_constraints c
          WHERE  c.owner = t.owner
          AND    c.table_name = t.table_name 
          AND    c.constraint_type = 'P' ) primary_key_constraints,
        ( SELECT count(*) 
          FROM   all_constraints c
          WHERE  c.owner = t.owner
          AND    c.table_name = t.table_name 
          AND    c.constraint_type = 'R' ) foreign_key_constraints,
        ( SELECT count(*) 
          FROM   all_constraints c
          WHERE  c.owner = t.owner
          AND    c.table_name = t.table_name 
          AND    c.constraint_type = 'U' ) unique_constraints,
        ( SELECT count(*) 
          FROM   all_constraints c
          WHERE  c.owner = t.owner
          AND    c.table_name = t.table_name 
          AND    c.constraint_type = 'C' ) check_constraints,
        ( SELECT count(*) 
          FROM   all_constraints c
          WHERE  c.owner = t.owner
          AND    c.table_name = t.table_name 
          AND    c.constraint_type NOT IN ('P','R','U', 'C') ) other_constraints,
          -- Indexes
        ( SELECT count(*)
          FROM   all_indexes i
          WHERE  i.table_owner = t.owner
          AND    i.table_Name = t.table_name
          AND    i.uniqueness = 'UNIQUE' ) unique_indexes,
        ( SELECT count(*)
          FROM   all_indexes i
          WHERE  i.table_owner = t.owner
          AND    i.table_Name = t.table_name
          AND    i.uniqueness = 'NONUNIQUE' ) nonunique_indexes,
        -- Triggers
        ( SELECT count(*)
          FROM   all_triggers tr
          WHERE  tr.table_owner = t.owner
          AND    tr.table_name = t.table_Name ) triggers
FROM     all_tables t
WHERE    t.table_name = 'MY_FAVORITE_TABLE';

您可以使用 DBA_DEPENDENCIES 添加包和视图等,使用 (REFERENCED_OWNER, REFERENCED_NAME, REFERENCED_TYPE) 列加入。但这只会计算将 直接 引用到表的对象。例如,它不会计算通过同义词引用表的包。例如,它不会计算引用引用表的视图的包(尽管视图会被计算在内)。

【讨论】:

    【解决方案2】:

    试试这个:

    -- This SQL will show all dependent objects for a table including FK references and index references
    -- =================================================================================================
    -- constraint references
    select to_char(sysdate, 'DD-MON-YYYY') date_evaluated
    , cons.owner ||'.'|| cons.table_name || ' (TABLE)' as object
    , 'FK Reference to' relationship
    , col.owner ||'.'|| col.table_name || ' (TABLE)' as referenced_object
    , cons.owner owner
    , cons.table_name name
    , 'TABLE' type
    , col.owner referenced_owner
    , col.table_name referenced_name
    , 'TABLE' referenced_type
    from dba_cons_columns      col
    ,    dba_constraints       cons
    where 1=1
      and cons.owner = nvl(:object_owner, cons.owner)
      and cons.table_name = nvl(:object_name, cons.table_name)
      and cons.r_owner = col.owner
      and cons.r_constraint_name = col.constraint_name
    union
    -- object references from dba_dependencies
    select /*+ MATERIALIZE */ to_char(sysdate, 'DD-MON-YYYY') date_evaluated
    , referenced_owner || '.' || referenced_name || '(' || referenced_type || ')' as object
    , 'Referenced in' relationship
    , owner || '.' || name || '(' || type || ')' as referenced_object
    , referenced_owner
    , referenced_name
    , referenced_type
    , owner
    , name
    , type
    from dba_dependencies
    where 1=1
      and name not like 'BIN$%'
      and referenced_name not like 'BIN$%'
      and type in ('TABLE', 'MATERIALIZED VIEW', 'VIEW', 'PACKAGE', 'TRIGGER', 'INDEX')
      and referenced_type in ('TABLE', 'MATERIALIZED VIEW', 'VIEW')
      and referenced_owner like nvl(:object_owner, referenced_owner)
      and referenced_name like nvl(:object_name, referenced_name)
    union
    -- object references from dba_indexes
    select /*+ MATERIALIZE */ to_char(sysdate, 'DD-MON-YYYY') date_evaluated
    , table_owner || '.' || table_name || '(TABLE)' as object
    , 'Referenced in' relationship
    , owner || '.' || index_name || '(INDEX)' as referenced_object
    , table_owner referenced_owner
    , table_name referenced_name
    , '(TABLE)' referenced_type
    , owner owner
    , index_name name
    , '(INDEX)' type
    from dba_indexes
    where 1=1
      and table_owner like nvl(:object_owner, table_owner)
      and table_name like nvl(:object_name, table_name)
    ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-09
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 2017-08-05
      • 2014-08-14
      • 1970-01-01
      相关资源
      最近更新 更多