2013-05-31 60 views
3

我有一個參考約束,即使參考值確實在引用表中,也不會啓用。我仔細查看了約束腳本和兩個表中的拼寫。Oracle 11g:混淆約束

當我嘗試啓用約束時,返回的錯誤是'父鍵未找到'..我物理比較數據,所需的值確實在參考表中。

被引用的列設置爲主鍵並且已啓用。

涉及的過程涉及通過dblink從另一個模式/數據庫傳輸數據。

在數據傳輸的源表中,存在一個確實啓用的類似約束。

由於數據敏感性,無法真正發佈數據,只是希望能夠得到一些進一步檢查的想法。

任何意見或建議表示讚賞。

規範約束:

ALTER TABLE SR2.LOG ADD (
    CONSTRAINT FF1 
    FOREIGN KEY (NOTCH_ID) 
    REFERENCES SR2.NOTCH (ID) 
    DISABLE NOVALIDATE); 
+0

DO用戶保持對任何父母或子女的表插入數據,而你驗證FK?這是在生產數據庫上完成的嗎? –

+0

源表是生產,但引用的值可以在參考表中看到。 –

+0

也許用戶進程插入數據的順序可能是原因。比如先插入子表。只是一個想法,但。你有沒有嘗試EXCEPTIONS子句? –

回答

5

有對於一個Oracle內置的解決方案。 您可以使用ALTER TABLE的例外條款:

-- parent table 
    create table t1 (col1 number primary key); 

    insert into t1 values (1); 
    insert into t1 values (2); 
    insert into t1 values (3); 
    commit; 

    -- child table 
    create table t2 (col1 number); 

    insert into t2 values (1); 
    insert into t2 values (2); 
    insert into t2 values (3); 
    insert into t2 values (4); -- bad data 
    commit; 

    -- You create a table for the exceptions 
    create table excepts (row_id rowid, 
         owner varchar2(30), 
         table_name varchar2(30), 
         constraint varchar2(30)); 

    -- you still get error 
    alter table t2 add constraint f2 foreign key (col1) references t1 
    exceptions into excepts ; 

    -- but bad data will be here 
    -- please notice its 'ROW_ID' from the second table 
    select t2.* 
    from t2, 
     excepts 
    where t2.rowid = excepts.row_id; 
+0

您仍然可以在ENABLE VALIDATE語句中使用它: alter table t2啓用驗證約束f2 異常轉換爲excepts; –

+0

謝謝Felipe。 –

+0

+1總是有一些新的學習。有沒有辦法在'alter table'期間防止錯誤?你知道支持類似語法的RDBMS嗎? – Beryllium