3

I have a referential constraint that will not enable, even though the referenced values are indeed in the referenced table. I have closely looked over the spelling in the constraint script and in both tables.

When I try to enable the constraint, the error returned is 'parent key not found'.. I physically compared the data, and the values needed are indeed in the referenced table.

The referenced column is set as Primary Key and that is enabled.

The involved process concerns a data load/transfer from another schema/database via a dblink.

In the source tables for the data transfer, there is a similar constraint that is indeed enabled.

Can't really post the data due to data sensitivity, was just hoping I could get some ideas of further things to check.

Any ideas or suggestions appreciated.

Code for constraint:

  ALTER TABLE SR2.LOG ADD (
  CONSTRAINT FF1 
   FOREIGN KEY (NOTCH_ID) 
   REFERENCES SR2.NOTCH (ID)
    DISABLE NOVALIDATE);
3
  • Do users keep inserting data on any of parent or child tables while you validate the FK? Is this being done on a production database? May 31, 2013 at 23:02
  • The source table is production, but the referenced values can be seen in the referenced table. May 31, 2013 at 23:12
  • Perhaps the order in which a user process inserts data might be the cause. Things like inserting on the child table first. Just an idea though. Did you try the EXCEPTIONS clause? May 31, 2013 at 23:15

1 Answer 1

5

There is an Oracle built-in solution for that. You could use the EXCEPTIONS clause of the 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;
3
  • You could still use it in the ENABLE VALIDATE statement: alter table t2 enable validate constraint f2 exceptions into excepts ; May 31, 2013 at 21:53
  • +1 There is always something new to learn. Is there a way to prevent the error during alter table? Do you know ther RDBMS which support a similar syntax?
    – Beryllium
    Jun 1, 2013 at 5:59
  • @Beryllium The error is for you to know something went wrong. I just work with Oracle, I have no idea if other RDBMS has this feature. Jun 1, 2013 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.