While you are trying to truncate table you can hit ORA-02266: error.
In this post I am going to make demo and share solution steps.
I assume that We have 2 tables. Test and Test2. Those table have relation between each other.
Let us try to truncate test table first
SQL>truncate table test; truncate table test * ERROR at line 1: ORA-02266: unique/primary keys in table referenced by enabled foreign keys
It gives some records have relation with some other records.
There are 2 options now.
1. Easy way is using The TRUNCATE TABLE … CASCADE command succeeds and recursively truncates all the dependent tables.
2.Other way which is related with this post. Now let us follow option 2
Let us disable constraints first; SQL>select 'alter table '||a.owner||'.'||a.table_name||' disable constraint '||a.constraint_name||';' from all_constraints a, all_constraints b where a.constraint_type = 'R' and a.r_constraint_name = b.constraint_name and a.r_owner = b.owner and a.owner= 'USER1,USER2'; -- ---> If you want to drop many tables under different schemas 'ALTERTABLE'||A.OWNER||'.'||A.TABLE_NAME||'DISABLECONSTRAINT'||A.CONSTRAINT_NAME||';' --------------------------------------------------------------------------------------------------------------------------------------------------- alter table GUNES.TEST disable constraint FK_NO; alter table GUNES.TEST2 disable constraint FK_NO2; SQL> alter table GUNES.TEST disable constraint FK_NO; Table altered. SQL>truncate table test; Table truncated.
Now Let us enable constraints again;
SQL>select 'alter table '||a.owner||'.'||a.table_name||' enable constraint '||a.constraint_name||';' from all_constraints a, all_constraints b where a.constraint_type = 'R' and a.r_constraint_name = b.constraint_name and a.r_owner = b.owner and a.owner= 'USER1,USER2'; -- ---> If you want to drop many tables under different schemas 'ALTERTABLE'||A.OWNER||'.'||A.TABLE_NAME||'ENABLECONSTRAINT'||A.CONSTRAINT_NAME||';' ---------------------------------------------------------------------------------------------------------------------------------- alter table GUNES.TEST enable constraint FK_NO; SQL>alter table GUNES.TEST enable constraint FK_NO; * ERROR at line 1: ORA-02298: cannot validate (GUNES.FK_NO) - parent keys not found
As you can see now We got ORA-02298.So We are hitting this error because an alter table validating constraint failed because the table has orphaned child records.
So What is solutions:
Solution 1: delete child record
Solution 2: enable constraint with novalidate
SQL>select 'alter table '||a.owner||'.'||a.table_name||' enable novalidate constraint '||a.constraint_name||';' from all_constraints a, all_constraints b where a.constraint_type = 'R' and a.r_constraint_name = b.constraint_name and a.r_owner = b.owner and a.owner= 'USER1,USER2'; -- ---> If you want to drop many tables under different schemas 'ALTERTABLE'||A.OWNER||'.'||A.TABLE_NAME||'ENABLENOVALIDATECONSTRAINT'||A.CONSTR -------------------------------------------------------------------------------- alter table GUNES.TEST enable novalidate constraint FK_NO; SQL> alter table GUNES.TEST enable novalidate constraint FK_NO; Table altered.