How can I find which tables reference a given table in Oracle SQL Developer?

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

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?

回答1:

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 schema, and r_table_name is the table for which you are looking for references. The names are case sensitive


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.



回答2:

To add this to SQL Developer as an extension do the following:

  1. Save the below code into an xml file (e.g. fk_ref.xml):
  1. Add the extension to SQL Developer:

    • Tools > Preferences
    • Database > User Defined Extensions
    • Click "Add Row" button
    • In Type choose "EDITOR", Location is where you saved the xml file above
    • Click "Ok" then restart SQL Developer
  2. Navigate to any table and you should now see an additional tab next to SQL one, labelled FK References, which displays the new FK information.

  3. Reference



回答3:

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


回答4:

SQL Developer 4.1, released in May of 2015, added a Model tab which shows table foreign keys which refer to your table in an Entity Relationship Diagram format.



回答5:

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


回答6:

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 =     AND c.constraint_TYPE = 'R'; 


回答7:

SELECT DISTINCT table_name,                  constraint_name,                  column_name,                  r_table_name,                  position,                  constraint_type  FROM   (SELECT uc.table_name,                 uc.constraint_name,                 cols.column_name,                 (SELECT table_name                  FROM   user_constraints                  WHERE  constraint_name = uc.r_constraint_name) r_table_name,                 (SELECT column_name                  FROM   user_cons_columns                  WHERE  constraint_name = uc.r_constraint_name                         AND position = cols.position)           r_column_name,                 cols.position,                 uc.constraint_type          FROM   user_constraints uc                 inner join user_cons_columns cols                         ON uc.constraint_name = cols.constraint_name          WHERE  constraint_type != 'C')  START WITH table_name = '&&tableName'             AND column_name = '&&columnName'  CONNECT BY NOCYCLE PRIOR table_name = r_table_name                           AND PRIOR column_name = r_column_name;  


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!