在数据库里面使用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. andriod 手机按键检测事件 onKeyDown() 简述

    函数原型: public boolean onKeyDown(int keyCode, KeyEvent event); 第一个参数是用户按下键时,当前所接收到的按键代号: 第二个参数是按键事件的对象 ...

  2. Android系统build.prop文件

    # begin build properties (开始设置系统性能) # autogenerated by buildinfo.sh (通过设置形成系统信息) ro.build.id=GRI40 ( ...

  3. Bootstrap 3.0正式版发布!

    Bootstrap是Twitter推出的一个开源的用于前端开发的工具包,包含了丰富的Web组件.根据这些组件,开发者可以快速的搭建一个漂亮.功能完备的网站.在经过Bootstrap 3 RC版的测试和 ...

  4. Hibernate ——二级缓存

    一.Hibernate 二级缓存 1.Hibernate 二级缓存是 SessionFactory 级别的缓存. 2.二级缓存分为两类: (1)Hibernate内置二级缓存 (2)外置缓存,可配置的 ...

  5. Windows下程序启动时出现0xc000007b错误的解决方案

    一.背景介绍 不久前用Qt开发了一款小工具在公司使用,Debug运行时一切正常.可是当Release编译发布打包之后,运行时弹出这样的提示框: 略一思考,首先排除了DLL缺失的可能性.我知道Qt应用程 ...

  6. 从View向Controller传递复杂类型Json

    mvc给我们提供多种controller中读取view数据的方法 1.从Ruquest["name"]中直接读取 2.将表单中name名称直接写在Action的参数列表中 3.将表 ...

  7. [转]9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路

    1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...

  8. ASP.NET Core开发-获取所有注入(DI)服务

    获取ASP.NET Core中所有注入(DI)服务,在ASP.NET Core中加入了Dependency Injection依赖注入. 我们在Controller,或者在ASP.NET Core程序 ...

  9. C#如何创建泛型类T的实例

    最近在学历基类的写法时,遇到了一个问题:如何怎么创建一个泛型类T的实例呢?     废话不多说了,直接上代码吧,目前发现三种方法,先贴上,以后再总结,希望能帮助跟我遇到同样问题的朋友. 方法一,通过外 ...

  10. VGA colorbar显示

    module VGAcolorbar(clk,rst,hsync,vsync,vga_r,vga_g,vga_b ); input clk; input rst; output hsync; outp ...