Ошибка ora 02298

I essentially have 2 tables, one named table1 and another called table2.

I want to insert a foreign key into table2 and have it make sure it is linked as a parent to table1 (since both tables have the same column called: EMPNO).

This is what I tried:

ALTER TABLE table2 
ADD FOREIGN KEY (EMPNO) 
REFERENCES table1(EMPNO)  

however I receive an error from my live SQL when running it:

ORA-02298: cannot validate (SQL_EOTBMPLTBLKHWFZRYEHITBYIH.) — parent keys not found

mroach's user avatar

mroach

2,3831 gold badge21 silver badges28 bronze badges

asked Feb 19, 2017 at 2:46

Orcka's user avatar

2

This error means that table2 has foreign key values that do not exist in the parent table, table1. To identify them, run a query like this:

SELECT DISTINCT EMPNO FROM table2 WHERE EMPNO NOT IN (
    SELECT EMPNO FROM table1)

Fix those bad key values and then run your ALTER TABLE again.

answered Feb 19, 2017 at 3:25

mroach's user avatar

mroachmroach

2,3831 gold badge21 silver badges28 bronze badges

One of the classic questions…

But I really can’t find where the issue is in the parent table. I am currently working on a mini-version of the IMDB database. Our teacher tasked us with taking the original database we were given and make it into a smaller, and smarter version. It should follow this diagram:

Database Diagram

I have so far successfully made the COMPANY_NAME, MOVIE_COMPANY, MOVIE, GENRE and PLOT tables and is currently making KEYWORD. The table itself now exists, but I cannot place the Foreign constraint which indicates that the MOVIE_ID present in keyword is from the parent table MOVIE. I keep getting the following error:

ALTER TABLE KEYWORD
  ADD CONSTRAINT FK_MOVIE_ID_KEYWORD
    FOREIGN KEY (MOVIE_ID)
    REFERENCES MOVIE (ID)
Error report -
SQL Error: ORA-02298: cannot validate (DB_031.FK_MOVIE_ID_KEYWORD) - 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.
*Action:   Obvious <-- especially this part is annoying

I have looked around at similar questions and then checked my statement a few times, but I have so far been unable to see why this fails. The GENRE and PLOT tables are both empty in terms of data, as they are made for future expansion of the database. But KEYWORD already have a collection of KEYWORD, MOVIE_ID pairs.

EDIT: Request Schemas

Movie Schema

Keyword Schema

Find the row which causing violation of foreign key constraint in Oracle

Find the row which caused a violation of the foreign key when tried to enable the foreign key constraint:

To clean the rows that violate the foreign key constraint, You need to create an EXCEPTIONS table in the current schema. Use this script present in the RDBMS folder of Oracle Home: rdbms/admin/utlexcpt.sql.

Following are the steps to find the row that violate the foreign key constraint:

1. Create an exceptions table.

SQL> @?/rdbms/admin/utlexcpt.sql;

2. Populate the EXCEPTIONS table with the rows that violate the constraint, using the EXCEPTIONS INTO clause.

SQL> alter table table_name modify constraint fk_constraint_name validate exceptions into exceptions;

Note This statement while executing still throws the ORA-02298 error as long as there are rows that violate the constraint. The statement also inserts records into the EXCEPTIONS table for any bad rows.

  1. Check the Exception table for rows.
    You use the ROW_ID column of the EXCEPTIONS table to remove any records that violate the constraint. In Example, you find that only one row needs to be removed from the EMP table.
 SQL> SELECT * FROM EXCEPTIONS;
Example:
ROW_ID              OWNER  TABLE_NAME  CONSTRAINT
------------------  -----  ----------  --------------------
AAAFKQAABAAAK8JAAB  SCOTT  EMP         EMP_DEPT_FK

Example of Creating Foreign key and reproducing the error and solution:

--- Created Parent p1 and Child c1 table with foreign key established 

SQL> create table p1(id number primary key, name varchar2(100));
Table created.

SQL> create table c1(cid number primary key, pid number, constraint fk_c2 foreign key (pid) references p1(id));
Table created.

-- insert into parent and child table
SQL> insert into p1 values (1,'a');
1 row created.
SQL> insert into p1 values (2,'b');
1 row created.

SQL> insert into c1 values (2,5);
insert into c2 values (2,5)
*
ERROR at line 1:
ORA-02291: integrity constraint (SYS.FK_C2) violated - parent key not found

-- Disable the foreign key constraint to insert value
SQL> alter table c2 disable constraint FK_C2;
Table altered.

SQL> insert into c2 values (2,5);
1 row created.

--Enable the constraint throw the error 
SQL> alter table c2 enable constraint FK_C2;
alter table c2 enable constraint FK_C2
                                 *
ERROR at line 1:
ORA-02298: cannot validate (SYS.FK_C2) - parent keys not found

--Find the row causing problem to enable the constraint:
SQL> @?/rdbms/admin/utlexcpt.sql;

Table created.

--Run again the command using EXCEPTION table:
SQL> alter table c2 modify constraint FK_C2 validate exceptions into exceptions;
alter table c2 modify constraint FK_C2 validate exceptions into exceptions
                                 *
ERROR at line 1:
ORA-02298: cannot validate (SYS.FK_C2) - parent keys not found

--Check the EXCEPTION table having rowid which causing problem:
SQL> col owner for a10
SQL> col table_name for a10
SQL> col constraint for a20
SQL> select * from exceptions;

ROW_ID             OWNER      TABLE_NAME CONSTRAINT
------------------ ---------- ---------- --------------------
AAATBmAABAAAIGBAAA SYS        C2         FK_C2


-- Check the row in table 
SQL> select * from c2 where rowid in (select row_id from exceptions);

       CID        PID
---------- ----------
         2          5

May 1, 2021

I got ” ORA-02298 cannot validate parent keys ” error in Oracle database.

ORA-02298 cannot validate parent keys

Details of error are as follows.

ORA-02298 cannot validate parent keys

Cause: an alter table validating constraint failed because the table has child records.

Action: Obvious



cannot validate parent keys

This ORA-02298 errors are related with the constraint failed because the table has child records.

To solve this error, you need to delete the child records from the child table, then try again.

Or you need to insert the missing records to the parent table, then try again.

This problem is related with the parent – child ( foreign key relationship )

Do you want to learn Oracle Database for Beginners, then read the following articles.

Oracle Tutorial | Oracle Database Tutorials for Beginners ( Junior Oracle DBA )

 1,902 views last month,  4 views today

About Mehmet Salih Deveci

I am Founder of SysDBASoft IT and IT Tutorial and Certified Expert about Oracle & SQL Server database, Goldengate, Exadata Machine, Oracle Database Appliance administrator with 10+years experience.I have OCA, OCP, OCE RAC Expert Certificates I have worked 100+ Banking, Insurance, Finance, Telco and etc. clients as a Consultant, Insource or Outsource.I have done 200+ Operations in this clients such as Exadata Installation & PoC & Migration & Upgrade, Oracle & SQL Server Database Upgrade, Oracle RAC Installation, SQL Server AlwaysOn Installation, Database Migration, Disaster Recovery, Backup Restore, Performance Tuning, Periodic Healthchecks.I have done 2000+ Table replication with Goldengate or SQL Server Replication tool for DWH Databases in many clients.If you need Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS Consultancy and Training you can send my email adress [email protected].-                                                                                                                                                                                                                                                 -Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS ve linux Danışmanlık ve Eğitim için  [email protected] a mail atabilirsiniz.

When trying to add a foreign key to a table sometimes the error ORA-02298 is thrown.

Let’s have two tables a MyTable and a MyTableHistory connected by a historyid column from MyTable that points to the id column from MyTableHistory

Try then to add add foreign key like:

ALTER TABLE MyTableHistory add CONSTRAINT MyKey foreign key (default_id) references MyTable;

If we are in the case of the ORA-02298 error the following message will be given by Oracle:

Error report -
ORA-02298: cannot validate (MyKey) - 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.
*Action:   Obvious

Funny how someone marked the solution as “Obvious” 🙂 Not so obvious because I had to look around some time to figure out what was the problem.

This error means that you have ids in your MyTableHistory table that are not present in the MyTable table. The foreign key can therefore not be applied.

This can be checked by running a query like:

SELECT * FROM MyTableHistory WHERE id NOT IN (SELECT MyTable.historyid FROM MyTable);

If the above query returns some rows it means that at some point we deleted (cleaned) some rows from the MyTable without cleaninf also the refered entries from MyTableHistory.

Do do the proper clean-up I had to execute:

DELETE FROM MyTableHistory WHERE id NOT IN (SELECT MyTable.historyid FROM MyTable);

After the clean-up is done we then ca execute again:

ALTER TABLE MyTableHistory add CONSTRAINT MyKey foreign key (default_id) references MyTable;

Success !

Понравилась статья? Поделить с друзьями:
  • Ошибка ora 02291 integrity constraint
  • Ошибка ora 02049
  • Ошибка ora 01920
  • Ошибка ora 01691
  • Ошибка ora 01658