您有不匹配的外鍵,例如值:
create table t1 as
select 1 id from dual
create table t2 as
select 1 id, 1 fk from dual
alter table t1 add primary key(id);
alter table t2 add primary key(id);
alter table t2 add constraint t2_fk foreign key(fk) references t1(id)
alter table t2 disable constraint t2_fk
insert into t2 values (2, 2)
alter table t2 enable constraint t2_fk
ORA-02298: cannot validate (ODCS_DVLP.T2_FK) - parent keys not found
而且你可能已經刪除了父表中的行:
delete t1
1 row(s) deleted.
alter table t2 enable constraint t2_fk
ORA-02298: cannot validate (ODCS_DVLP.T2_FK) - parent keys not found
正如你可以看到我不能重新啓用外鍵,因爲t2
表的fk
列的值不存在於t1
表中以重新啓用,您必須將此值設置爲空:
update t2
set fk = null
where not exists (select null from t1 where t1.id = t2.fk)
1 row(s) updated.
alter table t2 enable constraint t2_fk
Table altered
或刪除其值的行不父表有:
delete t2
where not exists (select null from t1 where t1.id = t2.fk)
alter table t2 enable constraint t2_fk
Table altered
您已刪除,是由子記錄引用的記錄。因此你a)首先不能刪除記錄,並且b)現在外鍵不能被驗證,因爲不再給出參照完整性。您必須找到這些子記錄並將其刪除。 – gvenzl 2014-08-28 09:41:54