views:

2461

answers:

5

In Oracle SQL Developer, if I'm viewing the information on a table, I can view the constraints, which let me see the foreign keys (and thus which tables are referenced by this table), and I can view the dependencies to see what packages and such reference the table. But I'm not sure how to find which tables reference the table.

For example, say I'm looking at the emp table. There is another table emp_dept which captures which employees work in which departments, which references the emp table through emp_id, the primary key of the emp table. Is there a way (through some UI element in the program, not through SQL) to find that the emp_dept table references the emp table, without me having to know that the emp_dept table exists?

A: 

Taking a quick look I came across this link which talks about finding foreign key constraints in Oracle

Apologies if its not useful to you.

kevchadders
A: 

You may be able to query this from the ALL_CONSTRAINTS view:

SELECT table_name
  FROM ALL_CONSTRAINTS
 WHERE constraint_type = 'R' -- "Referential integrity"
   AND r_constraint_name IN (
           SELECT constraint_name
             FROM ALL_CONSTRAINTS
            WHERE table_name = 'EMP'
              AND constraint_type IN ('U', 'P') -- "Unique" or "Primary key"
         )
Adam Paynter
Foreign keys can reference Unique Keys, not just primary keys, also, the table name could be used in multiple schemas which would result in multiple matches. You need to use the 'Owner' column as well if you're going to use 'All_Constraints' and not 'User_Constraints'.
Mark Roddy
A: 

Replace [Your TABLE] with emp in the query below

select owner,constraint_name,constraint_type,table_name,r_owner,r_constraint_name
  from all_constraints 
 where constraint_type='R'
   and r_constraint_name in (select constraint_name 
                               from all_constraints 
                              where constraint_type in ('P','U') 
                                and table_name='[YOUR TABLE]');
lexu
A: 

How about something like this:

SELECT c.constraint_name, c.constraint_type, c2.constraint_name, c2.constraint_type, c2.table_name
  FROM dba_constraints c JOIN dba_constraints c2 ON (c.r_constraint_name = c2.constraint_name)
 WHERE c.table_name = <TABLE_OF_INTEREST>
   AND c.constraint_TYPE = 'R';
DCookie
+2  A: 

No. There is no such option available from Oracle SQL Developer.

You have to execute a query by hand or use other tool (For instance PLSQL Developer has such option). The following SQL is that one used by PLSQL Developer:

select table_name, constraint_name, status, owner
from all_constraints
where r_owner = :r_owner
and constraint_type = 'R'
and r_constraint_name in
 (
   select constraint_name from all_constraints
   where constraint_type in ('P', 'U')
   and table_name = :r_table_name
   and owner = :r_owner
 )
order by table_name, constraint_name

Where r_owner is the table which you are looking for references.


Be careful because on the reports tab of Oracle SQL Developer there is the option "All tables / Dependencies" this is from ALL_DEPENDENCIES which refers to "dependencies between procedures, packages, functions, package bodies, and triggers accessible to the current user, including dependencies on views created without any database links.". Then, this report have no value for your question.

FerranB