在数据库里面使用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. iOS开发——高级特性&Runtime运行时特性详解

    Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...

  2. java多线程--定时器Timer的使用

    定时的功能我们在手机上见得比较多,比如定时清理垃圾,闹钟,等等.定时功能在java中主要使用的就是Timer对象,他在内部使用的就是多线程的技术. Time类主要负责完成定时计划任务的功能,就是在指定 ...

  3. linux 安装 nginx 及反向代理配置

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,以下为Linux centos平台下安装nginx并配置反向代理的过程(采用源码安装的方式) 一:安装 ...

  4. 浅谈2D游戏设计模式2- WZ文件详解(UI.WZ)之MapLogin.img(1)

    玩过冒险岛的人都知道有一个WZ文件,那么这个WZ文件的内部是怎么样的呢,今天我就带大家一探究竟. 说实在的,我这是第一次接触WZ文件,但是却让我很震撼,为什么很震撼,因为这个居然是用VS2010写的! ...

  5. 自己动手丰衣足食之 jQuery 数量加减插件

    引言 做一个手机端的订单相关项目中,其中下订单时需要用到数量加减的控件,可以设置默认值,也可以设置最大值和最小值.使用jQuery这么长时间了,平时很少去编写属于自己的插件,现在编写的时候对立面的一些 ...

  6. Zend Studio主题的设置

    用了两年的Zend Studio,一直是默认的白色主题,让人都产生了视觉疲劳,今天才发现Zend Studio的设置方法: 步骤1:help–>install new software…

  7. 20 个看起来很棒的 Web UI 工具包

    程序员们比设计师更需要这些 UI 方面的内容: 1. Mini Reminders Mini Reminders 2. Transluscent UI elements Transluscent UI ...

  8. 基于Treeview控件遍历本地磁盘

    一.前言 Treeview控件常用于遍历本地文件信息,通常与Datagridview与ImageList搭配.ImageList控件用于提供小图片给TreeView控件,DatagridView通常显 ...

  9. 【Java每日一题】20161214

    package Dec2016; import java.util.ArrayList; import java.util.List; public class Ques1214 { public s ...

  10. nginx配置多个虚拟主机vhost

    在nginx下配置虚拟主机vhost非常方便.主要在nginx的配置文件nginx.conf中添加一个server即可 比如我想配置两个虚拟主机,通过域名linux.com和linux2.com访问, ...