在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误

SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM

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

有时候对应的中文错误提示为:ORA-02266: 表中的唯一/主键被启用的外部关键字引用,一般出现这个错误,是因为表中的主键被其它表的外键所引用,导致删除数据时出错。

此时,你可以通过下面脚本查看一下涉及该表主键的外键约束信息。

   1: select c1.table_name      as org_table_name,

   2:        c1.constraint_name as org_constraint_name,

   3:        c1.constraint_type as org_constriant_type,

   4:        n1.column_name     as org_colun_name,

   5:        c2.table_name      as ref_table_name,

   6:        c2.constraint_type as ref_constraint_type,

   7:        c2.constraint_name as ref_constraint_name,

   8:        n2.column_name     as ref_column_name

   9:   from dba_constraints  c1,

  10:        dba_constraints  c2,

  11:        dba_cons_columns n1,

  12:        dba_cons_columns n2

  13:  where c1.owner = 'OWNER_NAME'

  14:    and c1.table_name = 'TABLE_NAME'

  15:    and n1.constraint_name = c1.constraint_name

  16:    and n1.owner = c1.owner

  17:    and c2.constraint_type = 'R'

  18:    and c2.r_constraint_name = c1.constraint_name

  19:    and n2.owner = c2.owner

  20:    and n2.constraint_name = c2.constraint_name;

查询结果如下所示:

   1: SQL> select c1.table_name      as org_table_name,

   2:   2         c1.constraint_name as org_constraint_name,

   3:   3         c1.constraint_type as org_constriant_type,

   4:   4         n1.column_name     as org_colun_name,

   5:   5         c2.table_name      as ref_table_name,

   6:   6         c2.constraint_type as ref_constraint_type,

   7:   7         c2.constraint_name as ref_constraint_name,

   8:   8         n2.column_name     as ref_column_name

   9:   9    from dba_constraints  c1,

  10:  10         dba_constraints  c2,

  11:  11         dba_cons_columns n1,

  12:  12         dba_cons_columns n2

  13:  13   where c1.owner = 'ESCMOWNER'

  14:  14     and c1.table_name = 'SUBX_ITEM'

  15:  15     and n1.constraint_name = c1.constraint_name

  16:  16     and n1.owner = c1.owner

  17:  17     and c2.constraint_type = 'R'

  18:  18     and c2.r_constraint_name = c1.constraint_name

  19:  19     and n2.owner = c2.owner

  20:  20     and n2.constraint_name = c2.constraint_name;

  21:  

  22: ORG_TABLE_NAME   ORG_CONSTRAINT_NAME  ORG_CONSTRIANT_TYPE ORG_COLUN_NAME  REF_TABLE_NAME  REF_CONSTRAINT_TYPE REF_CONSTRAINT_NAME REF_COLUMN_NAME        

  23: --------------   ------------------- ------------------- ---------------- --------------  ------------------- ------------------- 

  24: SUBX_ITEM             PK_SUBX_ITEM           P               ITEM_ID         SUBX_DIMM            R                   FK_SUBX_DIMM                   ITEM_ID

  25:  

  26: SQL>

解决方法:先禁用表的主键约束,等截断后再启用

   1: SQL> ALTER TABLE ESCMOWNER.SUBX_ITEM DISABLE PRIMARY KEY CASCADE;

   2:  

   3:  

   4: SQL>TRUNCATE TABLE ESCMOWNER.SUBX_ITEM

   5:  

   6: SQL>ALTER TABLE ESCMOWNER.SUBX_ITEM ENABLE PRIMARY KEY;

   7:  

   8: SQL>ALTER TABLE ESCMOWNER.SUBX_DIMM ENABLE CONSTRAINT FK_SUBX_DIMM;

   9:  

注意事项:在ENABLE主键后不会自动恢复外键(没有cascade选项),因此需要手工对引用该键的约束进行ENABLE。

ORA-02266: unique/primary keys in table referenced by enabled foreign keys的更多相关文章

  1. ORA-02273: this unique/primary key is referenced by some foreign keys

    关于ORA-02273错误,以前还真没有仔细留意过.昨天遇到了这个问题,遂顺便总结一番,以后遇到这类问题就可以直接用下面方案解决.如下所示,我们首先准备一下测试环境. CREATE TABLE TES ...

  2. [Err] 1701 - Cannot truncate a table referenced in a foreign key constraint

    1.SET FOREIGN_KEY_CHECKS=0; 2.DELETE FROM ACT_RE_DEPLOYMENT where 1=1; 或者 truncate table ACT_RE_DEPL ...

  3. Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key.

    Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key. 1.rea ...

  4. ORA-02429: cannot drop index used for enforcement of unique /primary key

    相信不少人遇到过ORA-02429: cannot drop index used for enforcement of unique /primary key 这个错误,对应的中文提示"O ...

  5. oracle约束总结(not null/unique/primary key/foreign key/check)

    约束(constraint):对创建的表的列属性.字段进行的限制. 诸如:not null/unique/primary key/foreign key/check 作用范围:         ①列级 ...

  6. How can I list all foreign keys referencing a given table in SQL Server?

    How can I list all foreign keys referencing a given table in SQL Server?  how to check if columns in ...

  7. [PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres

    Every movie needs a director and every rented movie needs to exist in the store. How do we make sure ...

  8. Could not drop object 'student' because it is referenced by a FOREIGN KEY constraint

    1. Find foreign keys SELECT * FROM sys.foreign_keys WHERE referenced_object_id = object_id('Student' ...

  9. 外键 Foreign keys

    https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?vi ...

随机推荐

  1. [c++] Copy Control

    C++ allows the programmer to define how objects are to be copied, moved, assigned and destroyed. Tog ...

  2. grunt任务之seajs模块打包

    grunt与seajs grunt是前端流行的自定义任务的脚手架工具,我们可以使用grunt来为我们做一些重复度很高的事情,如压缩,合并,js语法检查等.通过定义grunt的配置文件Gruntfile ...

  3. java操作数据库增删改查的小工具1--TxQueryRunner

    在java程序中,一般使用jdbc连接数据库,比较麻烦,在看传智教程时学了一个工具类,用于简化与数据库之间的操作步骤,就是TxQueryRunner,他是QueryRunner的子类,用起来和他是一样 ...

  4. 【JUC】JDK1.8源码分析之ReentrantReadWriteLock(七)

    一.前言 在分析了锁框架的其他类之后,下面进入锁框架中最后一个类ReentrantReadWriteLock的分析,它表示可重入读写锁,ReentrantReadWriteLock中包含了两种锁,读锁 ...

  5. 30分钟?不需要,轻松读懂IL

    先说说学IL有什么用,有人可能觉得这玩意平常写代码又用不上,学了有个卵用.到底有没有卵用呢,暂且也不说什么学了可以看看一些语法糖的实现,或对.net理解更深一点这些虚头巴脑的东西.最重要的理由就是一个 ...

  6. .NET 扩展方法 (一)

    我还记得刚刚学编程的时候,老师经常会提到一句话:注意空指针.所以经常在某些“入口”位置,进行代码校验,空指针的判断就是其中的一项工作. string类型作为常用的数据类型,它在项目中出现的机率极高,所 ...

  7. SQL 性能优化

    当我看到sql执行很慢的时候就在想为什么这么慢? 不外乎数据大,sql语句复杂,没有索引. 如果要进行优化的话可以从对应的这三个问题出发: 看看表是否可以进行拆分成小表,拆分sql语句,建立适合的索引 ...

  8. 针对Asp.net MVC SEO的几点建议

    1. 引言 SEO 即搜索引擎优化,很多web开发人员本应该熟悉,至少需要了解的一个知识点.像百度.必应等搜索引擎其实一直都在进化.但是有些优化的技巧可能在短时间内不变. 今天就给大家介绍几个专门针对 ...

  9. de4dot3.14更新文件打包下载

    刚发现de4dot更新了,虽然只是10月份的文件更新,并未发布新的release,但好多人还不会编译... 关于de4dot有何功能就不再讲了. 本文主要提供编译通过后的打包文件下载. 首先下载de4 ...

  10. java堆和栈的区别

    java 的内存分为两类,一类是栈内存,一类是堆内存.栈内存是指程序进入一个方法时,会为这个方法单独分配一块私属存储空间,用于存储这个方法内部的局部变量,当这个方法结束时,分配给这个方法的栈会释放,这 ...