Organizational Research By

Surprising Reserch Topic

truncate table in oracle getting errors

truncate table in oracle getting errors  using -'sql,oracle,plsql'

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?

asked Oct 3, 2015 by vijayshukla80
0 votes

Related Hot Questions

5 Answers

0 votes

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 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

answered Oct 3, 2015 by manju bhargava
0 votes

The error message is telling you that there are other table(s) with a foreign key constraint referring to your table.

According to the Oracle docs

You cannot truncate the parent table of an enabled foreign key constraint. You must disable the constraint before truncating the table.

The syntax for disabling a foreign key is:

ALTER TABLE table_name disable CONSTRAINT constraint_name;

answered Oct 3, 2015 by okesh.badhiye
0 votes

this page offers a very good solution ...

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

I'm here copying from it the Solution:

  • Find the referenced ENABLED foreign key constraints and disable them.
  • truncate/delete from the table .
  • using any text editor .. just change disable to enable in the output you get from the query , then run it.

    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.status='ENABLED'
    and a.r_constraint_name = b.constraint_name
    and a.r_owner  = b.owner
    and b.table_name = upper('YOUR_TABLE');
answered Oct 3, 2015 by sanjaypal1983
0 votes

A typical approach to delete many rows with many constraints is as follows:

  • create mytable_new with all the columns but without constrains (or create constraints disabled);
  • copy whatever data you need from mytable to mytable_new.
  • enable constraints on mytable_new to see that everything is ok.
  • alter any constraints that reference mytable to reference mytable_new instead and see that everything is ok.
  • drop table mytable.
  • alter table mytable_new rename to mytable.

It's far faster than deleting a million records with many slow constraints.

answered Oct 3, 2015 by tejas lakhani
0 votes

As mentioned by the error message, you cannot truncate a table that is referenced by enabled foreign keys. If you really want to use the truncate DDL command, disable the foreign key constraint first, run the truncate command, and enable it back.

Reference: Difference between TRUNCATE, DELETE and DROP commands

answered Oct 3, 2015 by dahiyabecomp