日期:2014-05-16  浏览次数:20422 次

truncate table时发生ORA-02266错误

??????? 前段时间,我将测试环境的数据导入到开发数据库后,发现开发环境数据库的数据量太大(千万级别),应用在开发环境(因开发数据库的性能远比不上测试数据库的性能)太慢甚至出现有些功能没法正常使用的问题,于是又急需将开发数据库中的数据删除至百万以下级别。

??????? 可问题来了:千万级别数据量的表用delete肯定不合适,因为数据量太大,于是想到truncate。但在truncate过程中发生了ORA-02266错误,这个是我要重点小结的。

??????? 先说我的整个思路:

????????1.建表结构和原来一模一样的表,将想要的数据备份在此表中,语法如下所示:

create table temp_tableName as select * from tableName where code like 'XXX%';

insert into temp_tableName select * from tableName where code like 'YYY%' or code like 'ZZZ%' or code like 'TTT%';

??????? 2.truncate清除数据

????????由于表之间存在主外链关联关系,所以必须先将有外链关联的主键对应的表先truncate掉

??????? 3.恢复数据

insert into tableName select * from temp_tableName where condition = 'KKK';

?

truncate过程中发生ORA-02266错误:

truncate table tableName;

报如下错误:

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

解决办法:

1.失效主键

alter table tableName disable primary key cascade;

2.truncate表数据

truncate table tableName;

3.恢复主键

alter table tableName enable primary key;