Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-25 · container image gvenzl/oracle-free:23-slim
When you add a foreign key with ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY, or turn a disabled one back on with ENABLE CONSTRAINT, Oracle checks every existing row in the child table. If even one row points to a parent key that does not exist, the statement fails with ORA-02298 and the constraint is not created or enabled.
The message names the constraint, not the bad rows, so the first step is always a NOT EXISTS query to find them. The three cases below were run on a real database.
01Adding a foreign key to a table that already has orphan rows
emp had three rows with dept_id 10, 20 and 30, but dept only had 10 and 20. ALTER TABLE emp ADD CONSTRAINT emp_dept_fk FOREIGN KEY (dept_id) REFERENCES dept (dept_id) failed with ORA-02298.
A NOT EXISTS query against dept returned the culprit: emp_id 3, PARK, dept_id 30. After updating that row to an existing department (20) and committing, the same ADD CONSTRAINT succeeded.
Setup
CREATE TABLE dept (dept_id NUMBER PRIMARY KEY, dept_name VARCHAR2(20));
INSERT INTO dept VALUES (10, 'SALES');
INSERT INTO dept VALUES (20, 'IT');
CREATE TABLE emp (emp_id NUMBER PRIMARY KEY, name VARCHAR2(20), dept_id NUMBER);
INSERT INTO emp VALUES (1, 'KIM', 10);
INSERT INTO emp VALUES (2, 'LEE', 20);
INSERT INTO emp VALUES (3, 'PARK', 30);
COMMIT;Table created.
1 row created.
1 row created.
Table created.
1 row created.
1 row created.
1 row created.
Commit complete.ALTER TABLE emp ADD CONSTRAINT emp_dept_fk FOREIGN KEY (dept_id) REFERENCES dept (dept_id);ALTER TABLE emp ADD CONSTRAINT emp_dept_fk FOREIGN KEY (dept_id) REFERENCES dept (dept_id)
*
ERROR at line 1:
ORA-02298: cannot validate (LAB_A.EMP_DEPT_FK) - parent keys not found
Help: https://docs.oracle.com/error-help/db/ora-02298/SELECT e.emp_id, e.name, e.dept_id FROM emp e
WHERE e.dept_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM dept d WHERE d.dept_id = e.dept_id); EMP_ID NAME DEPT_ID
---------- ------------------------------ ----------
3 PARK 30
1 row selected.UPDATE emp SET dept_id = 20 WHERE emp_id = 3;
COMMIT;1 row updated.
Commit complete.Whether you fix the child value, set it to NULL or add the missing parent depends on the data. The query only tells you which rows to look at.
ALTER TABLE emp ADD CONSTRAINT emp_dept_fk FOREIGN KEY (dept_id) REFERENCES dept (dept_id);Table altered.02Old data must stay as it is, but you still want the foreign key
order_hist had an old row with product_id 999, which is not in product. Adding the foreign key normally failed with ORA-02298.
Adding it with ENABLE NOVALIDATE succeeded. user_constraints showed ENABLED / NOT VALIDATED: the constraint is on, but existing rows were not checked. Inserting a new row with product 200 worked, and the old 999 row was still there.
Setup
CREATE TABLE product (product_id NUMBER PRIMARY KEY);
INSERT INTO product VALUES (100);
INSERT INTO product VALUES (200);
CREATE TABLE order_hist (order_id NUMBER PRIMARY KEY, product_id NUMBER);
INSERT INTO order_hist VALUES (1, 100);
INSERT INTO order_hist VALUES (2, 999);
COMMIT;Table created.
1 row created.
1 row created.
Table created.
1 row created.
1 row created.
Commit complete.ALTER TABLE order_hist ADD CONSTRAINT order_hist_prod_fk FOREIGN KEY (product_id) REFERENCES product (product_id);ALTER TABLE order_hist ADD CONSTRAINT order_hist_prod_fk FOREIGN KEY (product_id) REFERENCES product (product_id)
*
ERROR at line 1:
ORA-02298: cannot validate (LAB_A.ORDER_HIST_PROD_FK) - parent keys not found
Help: https://docs.oracle.com/error-help/db/ora-02298/ALTER TABLE order_hist ADD CONSTRAINT order_hist_prod_fk FOREIGN KEY (product_id) REFERENCES product (product_id) ENABLE NOVALIDATE;
COLUMN constraint_name FORMAT A20
SELECT constraint_name, status, validated FROM user_constraints WHERE constraint_name = 'ORDER_HIST_PROD_FK';Table altered.
CONSTRAINT_NAME STATUS VALIDATED
-------------------- ------------------------------ -------------
ORDER_HIST_PROD_FK ENABLED NOT VALIDATED
1 row selected.The orphan row is kept as is, so joins against the parent table will still miss it. Use this only when you have decided to leave the old data alone.
INSERT INTO order_hist VALUES (3, 200);
COMMIT;
SELECT order_id, product_id FROM order_hist ORDER BY order_id;1 row created.
Commit complete.
ORDER_ID PRODUCT_ID
---------- ----------
1 100
2 999
3 200
3 rows selected.03Re-enabling a foreign key after a bulk load with the constraint disabled
invoice_cust_fk was disabled, then four invoices were loaded. Two of them had cust_id 3, which was not in customer. ALTER TABLE invoice ENABLE CONSTRAINT invoice_cust_fk failed with ORA-02298.
Grouping the orphans by cust_id showed one missing key (3, 2 rows). After inserting customer 3 and committing, the ENABLE succeeded.
Setup
CREATE TABLE customer (cust_id NUMBER PRIMARY KEY);
INSERT INTO customer VALUES (1);
INSERT INTO customer VALUES (2);
CREATE TABLE invoice (inv_id NUMBER PRIMARY KEY, cust_id NUMBER CONSTRAINT invoice_cust_fk REFERENCES customer (cust_id));
ALTER TABLE invoice DISABLE CONSTRAINT invoice_cust_fk;
INSERT INTO invoice VALUES (1, 1);
INSERT INTO invoice VALUES (2, 2);
INSERT INTO invoice VALUES (3, 3);
INSERT INTO invoice VALUES (4, 3);
COMMIT;Table created.
1 row created.
1 row created.
Table created.
Table altered.
1 row created.
1 row created.
1 row created.
1 row created.
Commit complete.ALTER TABLE invoice ENABLE CONSTRAINT invoice_cust_fk;ALTER TABLE invoice ENABLE CONSTRAINT invoice_cust_fk
*
ERROR at line 1:
ORA-02298: cannot validate (LAB_A.INVOICE_CUST_FK) - parent keys not found
Help: https://docs.oracle.com/error-help/db/ora-02298/SELECT i.cust_id, COUNT(*) AS cnt FROM invoice i
WHERE NOT EXISTS (SELECT 1 FROM customer c WHERE c.cust_id = i.cust_id)
GROUP BY i.cust_id; CUST_ID CNT
---------- ----------
3 2
1 row selected.INSERT INTO customer VALUES (3);
COMMIT;1 row created.
Commit complete.Grouping by the key shows how many distinct parents are missing, which is usually much fewer than the number of bad rows.
ALTER TABLE invoice ENABLE CONSTRAINT invoice_cust_fk;Table altered.FAQ
What does ORA-02298 cannot validate - parent keys not found mean?
Some rows in the child table have a foreign key value that does not exist in the parent table, so Oracle cannot create or enable the foreign key.
How do I find the rows causing ORA-02298?
Query the child table with WHERE fk_col IS NOT NULL AND NOT EXISTS (SELECT 1 FROM parent p WHERE p.key = c.fk_col).
Can I add a foreign key without checking existing data?
Yes, with ENABLE NOVALIDATE. The constraint is created as ENABLED / NOT VALIDATED and existing orphan rows stay in the table.
Why does ENABLE CONSTRAINT fail after a data load?
Rows loaded while the constraint was disabled were not checked. Find the missing parent keys, add them or fix the child rows, then enable again.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0