截断Oracle中的表时出现错误

截断Oracle中的表时出现错误

本文介绍了截断Oracle中的表时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是,当我在Oracle中运行以下命令时,遇到错误.

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

我发现,此mytable与其他表有关系.这就是为什么Truncate命令无法继续进行的原因.如何使用Truncate命令使用SQL脚本从myTable中删除数据?

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?

推荐答案

您必须将TRUNCATE语句交换为DELETE语句,速度较慢且已记录下来,但这是在有约束条件时执行的方法.

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

其中pk-of-table是要被截断的表的主键的名称

Where pk-of-table is the name of the primary key of the table being truncated

运行以上查询的输出.完成此操作后,请记住再次启用它们,只需将DISABLE CONSTRAINT更改为ENABLE CONSTRAINT

Run the output of the above query. When this has been done, remember to enable them again, just change DISABLE CONSTRAINT into ENABLE CONSTRAINT

这篇关于截断Oracle中的表时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:50