Oracle
 sql >> Base de données >  >> RDS >> Oracle

Comment découvrir les colonnes de clé primaire (ou unique) sous-jacentes à partir d'une vue Oracle

Vous pouvez trouver ces informations via la vue user_dependencies :

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.

Cordialement,
Rob.

MODIFIER :Pour les noms de colonne de vue possibles, vous pouvez utiliser cette requête. Notez qu'il n'y a aucune garantie qu'une telle colonne existe dans votre vue.

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.