While truncating a table in database, might face the error ORA-02266 unique key error.
PROBLEM:
SQL> truncate table DBACLASS.AAF_USER;
truncate table DBACLASS.AAF_USER
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
SOLUTION:
The primary key in the table( which we are truncating) , seems to be referring to a another child table with foreign key.
Run below script to get the parent table , child table relation
set lines 2999 COL CHILD_TABLE FOR A20 col CONSTRAINT_NAME for a26 col owner form a10 col FK_column form a15 col table_name form a30 select b.owner, b.table_name child_table, c.column_name FK_column, b.constraint_name from dba_constraints a, dba_constraints b, dba_cons_columns c where a.owner=b.r_owner and b.owner=c.owner and b.table_name=c.table_name and b.constraint_name=c.constraint_name and a.constraint_name=b.r_constraint_name and b.constraint_type='R' and a.owner='&owner' and a.table_name='&TABLE_NAME' and a.CONSTRAINT_TYPE='P';
OWNER CHILD_TABLE FK_COLUMN CONSTRAINT_NAME ---------- -------------------- --------------- -------------------------- DBACLASS AAF_USER_MEMBER MEMBER AAF_USER_FK1
So either disable or drop this foreign constraint and try to truncate.
SQL> alter table dbaclass.AAF_USER_MEMBER disable constraint AAF_USER_FK1; Table altered. SQL> truncate table dbaclass.AAF_USER; Table truncated.
Very nicely explained .. i had came accross one of the same error .. and able to fix using diff. approach as mentioned in this