ppshein ppshein - 1 year ago 108
SQL Question

Truncate table in Oracle getting errors

I got the problem is when I run following command in Oracle, I encounter the error.

Truncate table mytable;


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

I found that, this mytable has relationship with other tables. That's why Truncate command cannot proceed anymore. How to delete data from myTable with the SQL scripts using Truncate command?

Answer Source

You have to swap the TRUNCATE statement to DELETE statements, slower and logged but that's the way to do it when constraints are in place.

DELETE mytablename;

Either that or you can find the foreign keys that are referencing the table in question and disable them temporarily.

from user_constraints
where R_CONSTRAINT_NAME='<pk-of-table>';

Where pk-of-table is the name of the primary key of the table being truncated
Run the output of the above query. When done, remember to enable them again, just change DISABLE CONSTRAINTS to ENABLE CONSTRAINTS

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download