我尝试添加约束时遇到以下错误

时间:2017-11-01 20:56:51

标签: oracle foreign-keys ddl

我需要弄清楚sfpl_plan_desc中的哪些记录具有失败特定FK的值。

ALTER TABLE SFPL_PLAN_DESC  ADD CONSTRAINT SFPL_PLAN_DESC_FK4 FOREIGN KEY ( DOC_TYPE ,  PLAN_TYPE , WORK_FLOW )  REFERENCES SFFND_DOC_TYPE_DEF


ERROR at line 1:
ORA-02298: cannot validate (SFMFG.SFPL_PLAN_DESC_FK4) - parent keys not found

1 个答案:

答案 0 :(得分:1)

简单,

Select distinct 
       DOC_TYPE
     , PLAN_TYPE
     , WORK_FLOW
  from SFPL_PLAN_DESC c
 where not exists (select 1 from SFFND_DOC_TYPE_DEF p
                    where p.doc_type = c.doc_type
                      and p.plan_type = c.plan_type
                      and p.work_flow = c.work_flow)

应该显示丢失的外键。