Truncate Table In Oracle Getting Errors
Solution 1:
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.
select'ALTER TABLE '||TABLE_NAME||' DISABLE CONSTRAINT '||CONSTRAINT_NAME||';'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 this has been done, remember to enable them again, just change DISABLE CONSTRAINT into ENABLE CONSTRAINT
Solution 2:
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 Answer :
- 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');
Solution 3:
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;
Solution 4:
Issue:
Error “ORA-02266: unique/primary keys intable referenced by enabled foreign keys” when trying totruncate a table.
Error Message:
SQL>truncatetable TABLE_NAME;
truncatetable TABLE_NAME
*
ERROR at line 1:
ORA-02266: unique/primary keys intable referenced by enabled foreign keys
Solution: -- Find the referenced foreign key constraints.
SQL>select'alter table '||a.owner||'.'||a.table_name||' disable constraint '||a.constraint_name||';'2from all_constraints a, all_constraints b
3where a.constraint_type ='R'4and a.r_constraint_name = b.constraint_name
5and a.r_owner = b.owner
6and b.table_name ='TABLE_NAME';
'ALTER TABLE'||A.OWNER||'.'||A.TABLE_NAME||'DISABLE CONSTRAINT'||A.CONSTRAINT_NAME||';'---------------------------------------------------------------------------------------------------------altertable SCHEMA_NAME.TABLE_NAME_ATTACHMENT disable constraint CONSTRAINT_NAME;
altertable SCHEMA_NAME.TABLE_NAME_LOCATION disable constraint CONSTRAINT_NAME;
-- Disable them
altertable SCHEMA_NAME.TABLE_NAME_ATTACHMENT disable constraint CONSTRAINT_NAME;
altertable SCHEMA_NAME.TABLE_NAME_LOCATION disable constraint CONSTRAINT_NAME;
-- Run the truncate
SQL>truncatetable TABLE_NAME;
Table truncated.
-- Enable the foreign keys back
SQL>select'alter table '||a.owner||'.'||a.table_name||' enable constraint '||a.constraint_name||';'2from all_constraints a, all_constraints b
3where a.constraint_type ='R'4and a.r_constraint_name = b.constraint_name
5and a.r_owner = b.owner
6and b.table_name ='TABLE_NAME';
'ALTER TABLE'||A.OWNER||'.'||A.TABLE_NAME||'ENABLE CONSTRAINT'||A.CONSTRAINT_NAME||';'--------------------------------------------------------------------------------altertable SCHEMA_NAME.TABLE_NAME_ATTACHMENT enable constraint CONSTRAINT_NAME;
altertable SCHEMA_NAME.TABLE_NAME_LOCATION enable constraint CONSTRAINT_NAME;
-- Enable them
altertable SCHEMA_NAME.TABLE_NAME_ATTACHMENT enable constraint CONSTRAINT_NAME;
altertable SCHEMA_NAME.TABLE_NAME_LOCATION enable constraint CONSTRAINT_NAME;
Solution 5:
Oracle 12c introduced a feature to truncate a table that is a parent of a referential integrity constraint having ON DELETE rule.
Instead of truncate table tablename; use:
TRUNCATETABLE tablename CASCADE;
From Oracle truncate table documentation:
If you specify CASCADE, then Oracle Database truncates all child tables that reference table with an enabled ON DELETE CASCADE referential constraint. This is a recursive operation that will truncate all child tables, granchild tables, and so on, using the specified options.
Post a Comment for "Truncate Table In Oracle Getting Errors"