使用:Oracle SQL Developer
我需要一个Oracle SQL查询,该查询将返回所有引用父表Table Employee
的子表(Table Building
和Table Organizational unit
)中与外键相关的所有列
我的示例的结果如下所示:
选择所有表的所有列,这些列具有作为表Table Organizational unit
的外键引用的条目
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 answer
找到了所有的Referencing表和Constraint名称。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
对于我的示例,上面的查询给了我类似的东西:
"TABLE_NAME","CONSTRAINT_NAME","STATUS","OWNER"
"Table Employee","FK_CONSTRAINT","ENABLED","TESTSCHEMA"
"Table Building","FK_CONST","ENABLED","TESTSCHEMA"
现在,我知道所有引用父表Organizational unit
的子表。现在,我也想要一个查询,该查询将获取与外键匹配的所有表中的所有行。