简体   繁体   中英

Joining oracle metadata tables

I am trying to join the metadata tables to make a query for statistics on the table such as count of trigger, sequences, packages, procedures, views etc, but i cannot find a key to join the tables on. Here is my query

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;

I want to make one big query to pull back all the data from all the main metadata tables Thanks

Here is a start, showing constraints, indexes, and triggers...

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';

You could add packages and views, etc using DBA_DEPENDENCIES using the ( REFERENCED_OWNER , REFERENCED_NAME , REFERENCED_TYPE ) columns to join on. But that will only count objects referring directly to the table. It won't count, for example, packages that refer to the table through a synonym. It won't count, for example, packages that refer to a view that refers to the table (though the view would be counted).

Try this:

-- 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)
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM