简体   繁体   中英

How can I find the parent table of a foreign constraint key in oracle?

How can I find the parent table(s) of foreign constraint(s) on a table key in oracle? I'm needing to create a dropdown of all of the values that can be selected for this column I'm looking at and need to know the parent so I can look up its sibling values.

You can query this information from all_constraints (or user_constraints or dba_constraints , of course). Unfortunately, you can only retrieve the name of the constraint a foreign key refers to, so you'll have to use a sub query or a self join to retrieve the referring table:

SELECT r.table_name
FROM   user_constraints t
JOIN   user_constraints r ON t.r_constraint_name = r.constraint_name
WHERE  t.constraint_type = 'R' AND t.table_name = 'SOME_TABLE'

You can use the below query to get the parent table.

select * from all_constraints
where constraint_name in (select r_constraint_name from all_constraints
where table_name in 'TAB_NAME'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM