在数据库里面使用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. 如何通过一个类名找到它属于哪个jar包?

    最简单的方式: 如果用eclipse选中类名,然后ctrl shift T,就可看到包含比类的包了 最有效的方式: import java.net.URL; /** * 检查class文件属于哪个ja ...

  2. 1Z0-053 争议题目解析699

    1Z0-053 争议题目解析699 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 699.Your database is using a default temporary ta ...

  3. JavaScript 获取HTML中的CSS样式的属性以及值的的方法。

    <body> <div id="wow" style="font-size:10px; float:left"></div> ...

  4. jQuery方法position()与offset()区别

    参考别人写得比较明白的,红色部分为重点吧: 使用jQuery获取元素位置时,我们会使用position()或offset()方法,两个方法都返回一个包含两个属性的对象-左边距和上边距,它们两个的不同点 ...

  5. 魔方渗透系统安装VMtools教程

    虚拟机魔方渗透系统安装VMtools教程 1.开机登陆后,如图点击安装VMtools. 2.进入media文件夹: cd /media   查看mdia文件夹内容: ls   3.打开VMware T ...

  6. Visual Studio Code 1.0发布,支持中文在内9种语言

    Visual Studio Code 1.0发布,支持中文在内的9种语言:Simplified Chinese, Traditional Chinese, French, German, Italia ...

  7. C# 超时工具类 第二版

    附源码,没有附测试demo 之前的工具类:C# 给某个方法设定执行超时时间 /// <summary> /// 超时工具 /// </summary> public class ...

  8. [转]C#中的string.Format()的JS版本

    String.prototype.format = function (args) { var result = this; if (arguments.length > 0) { var re ...

  9. [Q&A] 类Range的PasteSpecial方法无效

    环境说明: VS2013(C#) + Office2013 Bug说明: range1.Copy(Type.Missing); range2.PasteSpecial(Excel.XlPasteTyp ...

  10. CSS3点赞动画特效源码下载

    体验效果:http://hovertree.com/texiao/jquery/62/ 效果图: 下载:http://hovertree.com/h/bjaf/1dvh9ym6.htm 特效库:htt ...