2014-08-28 39 views
-1

我需要刪除我的數據庫視圖之一中的一個條目,當我嘗試這樣做時,我收到了參照完整性錯誤消息。我不想刪除表中的條目,所以我決定禁用FK並刪除我的數據庫視圖中的條目,然後重新啓用FK。Oracle:禁用了FK,現在無法重新啓用FK

工作正常,直到我試圖重新啓用我的FK並收到以下錯誤消息。

Error report: 
SQL Error: ORA-02298: cannot validate (DB.ABCD) - parent keys not found 
02298. 00000 - "cannot validate (%s.%s) - parent keys not found" 
*Cause: an alter table validating constraint failed because the table has 
      child records. 

我知道這應該是顯而易見的,但我在這裏有點失落。任何幫助是appeciated。

+0

您已刪除,是由子記錄引用的記錄。因此你a)首先不能刪除記錄,並且b)現在外鍵不能被驗證,因爲不再給出參照完整性。您必須找到這些子記錄並將其刪除。 – gvenzl 2014-08-28 09:41:54

回答

2

您有不匹配的外鍵,例如值:

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 
+0

是的,你是完全正確的!當我刪除視圖中的條目時,我無意中刪除了父表中的一行(必須是數據庫觸發器,我不知道),當我檢查子表時,我試圖重新啓用約束條件看到它有與刪除行相關的條目。感謝您的幫助,非常感謝。 – 180cl 2014-08-28 10:39:24