0

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

Basudev Singh
  • 79
  • 2
  • 13
  • 1
    What do you mean by, for example, "count of packages on a table"? Before you start looking for a coded solution, the problem must make sense in non-computing terms. –  Feb 25 '20 at 14:22
  • 1
    What is wrong with the query above, it looks to me like it should work. Was there an error? how did this not meet your needs? – Hogan Feb 25 '20 at 14:25
  • I think the data you are missing (sequences, packages, etc.) is stored in the user_dependencies table... – Radagast81 Feb 25 '20 at 14:47
  • and you're not doing any counts, so you probably want to do some grouping on the individual tables? – thatjeffsmith Feb 25 '20 at 14:48
  • 1
    A query like this is going to suffer from a "fan trap" (https://stackoverflow.com/a/16425001/5174436). For example, table X has three constraints and four triggers -- you'll get 12 rows in your result set for table X, which probably isn't what you are looking for. Ask yourself this: "I want each row in my result set to represent one.... what?". You are probably looking for multiple small queries (possibly concatenated via `UNION ALL`) more than "one big query". – Matthew McPeak Feb 25 '20 at 15:34

2 Answers2

0

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

Matthew McPeak
  • 17,705
  • 2
  • 27
  • 59
0

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)
;
Roger Cornejo
  • 1,507
  • 1
  • 8
  • 7