Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to discover the underlying primary (or unique) key columns from an Oracle view

I was wondering whether there is a possibility for me to discover the underlying primary (or unique) key columns for all tables involved in an Oracle view. Here's an example to show what I mean:

CREATE TABLE t_a (
  id number(7),
  primary key(id)
);

CREATE VIEW v_a AS
SELECT * FROM t_a;

So by naming convention, I know that v_a.id is actually the primary key column of the underlying t_a table. Is there any way of formally discovering this information by using system views, such as SYS.ALL_CONSTRAINTS, SYS.USER_CONSTRAINTS, etc?

N.B:

  • The constraints are NOT on the view, but on the underlying table.
  • I'm not interested in the keys themselves, but in the columns of the view.
like image 609
Lukas Eder Avatar asked Feb 24 '23 15:02

Lukas Eder


2 Answers

You can find that information via the user_dependencies view:

SQL> CREATE TABLE t_a
  2  (   id number(7)
  3  ,   primary key(id)
  4  )
  5  /

Table created.

SQL> CREATE VIEW v_a AS SELECT * FROM t_a
  2  /

View created.

SQL> select c.constraint_name
  2    from user_dependencies d
  3       , all_constraints c
  4   where d.name = 'V_A'
  5     and d.referenced_type = 'TABLE'
  6     and d.referenced_link_name is null
  7     and d.referenced_owner = c.owner
  8     and d.referenced_name = c.table_name
  9     and c.constraint_type = 'P'
 10  /

CONSTRAINT_NAME
------------------------------
SYS_C0051559

1 row selected.

Regards,
Rob.

EDIT: For possible view column names, you can use this query. Note there is no guarantee that such a column exists in your view.

SQL> select c.constraint_name
  2       , 'V_' || substr(c.table_name,3) || '.' || cc.column_name possible_view_column
  3    from user_dependencies d
  4       , all_constraints c
  5       , all_cons_columns cc
  6   where d.name = 'V_A'
  7     and d.referenced_type = 'TABLE'
  8     and d.referenced_link_name is null
  9     and d.referenced_owner = c.owner
 10     and d.referenced_name = c.table_name
 11     and c.constraint_type = 'P'
 12     and c.owner = cc.owner
 13     and c.constraint_name = cc.constraint_name
 14  /

CONSTRAINT_NAME                POSSIBLE_VIEW_COLUMN
------------------------------ -------------------------------------------------------------
SYS_C0051561                   V_A.ID

1 row selected.
like image 63
Rob van Wijk Avatar answered Feb 28 '23 03:02

Rob van Wijk


SELECT column_name FROM user_ind_columns
  WHERE index_name =
   (SELECT index_name FROM user_constraints
      WHERE constraint_type='P' AND table_name='T_A')
ORDER BY column_position;

If you want to do it for all tables that a view depends on, then change the condition on table_name to something like:

table_name IN (SELECT referenced_name FROM user_dependencies
                 WHERE name='view_name' AND referenced_type='TABLE')
like image 45
Dave Costa Avatar answered Feb 28 '23 01:02

Dave Costa