Oracle 23.26ConstraintsLab-verified · 2026-11-27

ORA-02266: unique/primary keys in table referenced by enabled foreign keys

TRUNCATE was run on a parent table whose key is referenced by an enabled foreign key.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-02266: unique/primary keys in table referenced by enabled foreign keys

Jump to a cause

  1. 01The child table still has rows
  2. 02The FK is ON DELETE CASCADE, but plain TRUNCATE was used
  3. 03You want to reload only the parent table and keep the child rows

Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-27 · container image gvenzl/oracle-free:23-slim

TRUNCATE removes rows without checking them one by one, so Oracle refuses to truncate a parent table while an enabled foreign key points at it and child rows exist. The statement fails with ORA-02266. A plain DELETE would check each row instead.

Below are three common cases reproduced on a real database, with the fix we ran and its result.

01The child table still has rows

emp.dept_id had CONSTRAINT emp_dept_fk REFERENCES dept, and emp had one row for dept 10. TRUNCATE TABLE dept failed with ORA-02266.

We truncated the child first with TRUNCATE TABLE emp. After that, TRUNCATE TABLE dept succeeded, and user_constraints still showed EMP_DEPT_FK ENABLED, so the foreign key was left on.

Setup
Setuprun as lab_a
CREATE TABLE dept (dept_id NUMBER CONSTRAINT dept_pk PRIMARY KEY, name VARCHAR2(30));
CREATE TABLE emp (emp_id NUMBER PRIMARY KEY, dept_id NUMBER CONSTRAINT emp_dept_fk REFERENCES dept);
INSERT INTO dept VALUES (10, 'SALES');
INSERT INTO emp VALUES (1, 10);
COMMIT;
OUTPUT
Table created.


Table created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
TRUNCATE TABLE dept;
✕ ERROR
TRUNCATE TABLE dept
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
Help: https://docs.oracle.com/error-help/db/ora-02266/
Fixrun as lab_a
TRUNCATE TABLE emp;
OUTPUT
Table truncated.

This was tested on 26ai, where an empty child table is enough. On older releases TRUNCATE of the parent can still fail while the FK is enabled; in that case use the third method (disable the FK).

After the fixrun as lab_a
TRUNCATE TABLE dept;
SELECT constraint_name, status FROM user_constraints WHERE table_name = 'EMP' AND constraint_type = 'R';
✓ SUCCESS
Table truncated.


CONSTRAINT_NAME                STATUS
------------------------------ ------------------------------
EMP_DEPT_FK                    ENABLED

1 row selected.

02The FK is ON DELETE CASCADE, but plain TRUNCATE was used

emp_dept_fk was created with ON DELETE CASCADE. Even so, TRUNCATE TABLE dept failed with ORA-02266.

user_constraints showed EMP_DEPT_FK CASCADE in delete_rule. With that rule in place, TRUNCATE TABLE dept CASCADE succeeded and both tables were emptied: DEPT_ROWS 0, EMP_ROWS 0.

Setup
Setuprun as lab_a
CREATE TABLE dept (dept_id NUMBER CONSTRAINT dept_pk PRIMARY KEY, name VARCHAR2(30));
CREATE TABLE emp (emp_id NUMBER PRIMARY KEY, dept_id NUMBER CONSTRAINT emp_dept_fk REFERENCES dept ON DELETE CASCADE);
INSERT INTO dept VALUES (10, 'SALES');
INSERT INTO emp VALUES (1, 10);
COMMIT;
OUTPUT
Table created.


Table created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
TRUNCATE TABLE dept;
✕ ERROR
TRUNCATE TABLE dept
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
Help: https://docs.oracle.com/error-help/db/ora-02266/
Checkrun as lab_a
SELECT constraint_name, delete_rule FROM user_constraints WHERE table_name = 'EMP' AND constraint_type = 'R';
OUTPUT
CONSTRAINT_NAME                DELETE_RU
------------------------------ ---------
EMP_DEPT_FK                    CASCADE

1 row selected.

TRUNCATE ... CASCADE also empties the child tables, and TRUNCATE cannot be rolled back. It only works when the foreign keys are ON DELETE CASCADE.

After the fixrun as lab_a
TRUNCATE TABLE dept CASCADE;
SELECT (SELECT COUNT(*) FROM dept) AS dept_rows, (SELECT COUNT(*) FROM emp) AS emp_rows FROM dual;
✓ SUCCESS
Table truncated.


 DEPT_ROWS   EMP_ROWS
---------- ----------
         0          0

1 row selected.

03You want to reload only the parent table and keep the child rows

We wanted to empty and reload dept while keeping the row in emp. TRUNCATE TABLE dept failed with ORA-02266.

We ran ALTER TABLE emp DISABLE CONSTRAINT emp_dept_fk, then TRUNCATE TABLE dept succeeded. We inserted dept 10 again, committed, and ran ENABLE CONSTRAINT. The result was EMP_ROWS 1 and FK_STATUS ENABLED.

Setup
Setuprun as lab_a
CREATE TABLE dept (dept_id NUMBER CONSTRAINT dept_pk PRIMARY KEY, name VARCHAR2(30));
CREATE TABLE emp (emp_id NUMBER PRIMARY KEY, dept_id NUMBER CONSTRAINT emp_dept_fk REFERENCES dept);
INSERT INTO dept VALUES (10, 'SALES');
INSERT INTO emp VALUES (1, 10);
COMMIT;
OUTPUT
Table created.


Table created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
TRUNCATE TABLE dept;
✕ ERROR
TRUNCATE TABLE dept
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
Help: https://docs.oracle.com/error-help/db/ora-02266/
Fixrun as lab_a
ALTER TABLE emp DISABLE CONSTRAINT emp_dept_fk;
OUTPUT
Table altered.

ENABLE checks the existing child rows. If a parent key you needed was not reloaded, ENABLE fails, so reload every key the child rows use.

After the fixrun as lab_a
TRUNCATE TABLE dept;
INSERT INTO dept VALUES (10, 'SALES (reloaded)');
COMMIT;
ALTER TABLE emp ENABLE CONSTRAINT emp_dept_fk;
SELECT (SELECT COUNT(*) FROM emp) AS emp_rows, (SELECT status FROM user_constraints WHERE constraint_name = 'EMP_DEPT_FK') AS fk_status FROM dual;
✓ SUCCESS
Table truncated.


1 row created.


Commit complete.


Table altered.


  EMP_ROWS FK_STATU
---------- --------
         1 ENABLED

1 row selected.

FAQ

What does ORA-02266 mean?

You tried to TRUNCATE a table whose primary or unique key is referenced by an enabled foreign key. Clear the child, use CASCADE, or disable the FK first.

Why does DELETE work but TRUNCATE fails with ORA-02266?

DELETE checks each row against the foreign key. TRUNCATE does not check rows, so Oracle blocks it while an enabled FK references the table.

How do I truncate a parent table and its child tables together?

If the foreign keys are ON DELETE CASCADE, TRUNCATE TABLE parent CASCADE empties both. In our test both tables had 0 rows afterwards.

Can I truncate the parent table without deleting child rows?

Yes. Disable the child's foreign key, truncate and reload the parent, then enable the FK. ENABLE fails if a child row has no matching parent.

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