I'm trying to figure out how to look up the name of the column referenced by a foreign key.
Here's an example to demonstrate, keep in mind that I'm trying to look this up dynamically for hundreds of tables.
- WIDGET
- ID
- COMPONENT_ID
- ...
- PART
- ID
- NAME
- ....
WIDGET.COMPONENT_ID
is a foreign key for PART.ID
I have a query to find PART
(table name) but I can't figure out how to find PART.ID
from WIDGET_COMPONENT_ID
.
EDIT:
I'm trying to find out the referenced column name. I can find the table name but I can't figure out how to find the column name.
EDIT 2:
(sql from List of foreign keys and the tables they reference) For example, I can get the constraint name via:
SELECT c_pk.constraint_name, c_pk.table_name r_table_name
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
WHERE c.constraint_type = 'R'
AND a.table_name = 'WIDGET'
This gets me something like COMPONENT_ID_FK
and PART
. How to go from COMPONENT_ID_FK
to PART.ID
though I'm not sure.