在数据库里面使用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. JavaScript框架设计(四) 字符串选择器(选择器模块结束)

    JavaScript框架设计(四) 字符串选择器(选择器模块结束) 经过前面JavaScript框架设计(三) push兼容性和选择器上下文的铺垫,实现了在某一元素下寻找,现在终于进入了字符串选择器 ...

  2. java.util.concurrent包详细分析--转

    原文地址:http://blog.csdn.net/windsunmoon/article/details/36903901 概述 Java.util.concurrent 包含许多线程安全.测试良好 ...

  3. 关于《selenium2自动测试实战--基于Python语言》

    关于本书的类型: 首先在我看来技术书分为两类,一类是“思想”,一类是“操作手册”. 对于思想类的书,一般作者有很多年经验积累,这类书需要细读与品位.高手读了会深有体会,豁然开朗.新手读了不止所云,甚至 ...

  4. CSS padding margin border属性详解

    图解CSS padding.margin.border属性W3C组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落.列表.标题.图片以及层. ...

  5. web性能优化:详说浏览器缓存

    TOC 背景 浏览器的总流程图 一步一步说缓存 朴素的静态服务器 设置缓存超时时间 html5 Application Cache Last-Modified/If-Modified-Since Et ...

  6. entity framework 删除数据库出现错误的解决方法--最土但是很有效的方法

    无法删除数据库,因为该数据库当前正在使用. public ChinaerContext() : base("name=ContextConn") { // Database.Set ...

  7. 图片全部加载完成之后再显示页面ui,公司项目里用上,自己写的几行代码

    说明: -----onload事件   这里我并没有考虑ie的兼容性 因为项目是移动端的: -----求大神指正~ -----自己测试正常 页面没加载完之前会有一个提示 /************** ...

  8. 【C#】分享一个可携带附加消息的增强消息框MessageBoxEx

    --------------201507160917更新--------------- 无意中发现标准消息框在Windows7是有声音的,只是在Windows server 2008(R2)无声,而我 ...

  9. CSS基础之居中显示

    这些天忙完了一些项目后,终于有时间来总结一下了.自己就把做项目过程中的体会和理解到的一些东西和大家分享一下.有错请指正!! 在css中,元素居中显示,是基础也是一个小难点.我们经常不知为何总是不能把元 ...

  10. 增加线程异步发送消息的方法一(Thread)

    @RequestMapping(value="order/updateOrder.do") public String updateOrder(HttpServletRequest ...