Using: Oracle SQL Developer
Hi I try to explain my Problem with a simple example:
I need a Oracle SQL Query that returns me all Foreign Key related columns from all child tables ( Table Employee
and Table Building
) that reference the parent table Table Organizational unit
The result for my example would look like this:
SELECT All COLUMNS OF All Tables THAT have an entry referenced as foreign key for table Table Organizational unit
WHERE Table Organizational unit.Code = 'HR'
--Result
Table Building
ID , ORG_UNIT_REF, Building Type
152, 2 , Main Building
Table Employee
ID, ORG_UNIT_REF, Employee Name
13, 2 , Max Doe
This means all Tables with their table name, column names and matching column contents have to be printed out.
I already found all the Referening tables and Constraint names by using this answer
SELECT a.table_name, a.column_name, a.constraint_name, c.owner,
-- referenced pk
c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
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 = :TableName
For my example the above query gives me something like that:
"TABLE_NAME","CONSTRAINT_NAME","STATUS","OWNER"
"Table Employee","FK_CONSTRAINT","ENABLED","TESTSCHEMA"
"Table Building","FK_CONST","ENABLED","TESTSCHEMA"
Now I know all the child tables that reference my parent table Organizational unit
. Now I also want a query that fetches me all the rows in all the tables where the foreign key matches.