ORA-02266: unique/primary keys in table referenced by enabled foreign keys
在数据库里面使用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的更多相关文章
- ORA-02273: this unique/primary key is referenced by some foreign keys
关于ORA-02273错误,以前还真没有仔细留意过.昨天遇到了这个问题,遂顺便总结一番,以后遇到这类问题就可以直接用下面方案解决.如下所示,我们首先准备一下测试环境. CREATE TABLE TES ...
- [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 ...
- 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 ...
- 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 ...
- oracle约束总结(not null/unique/primary key/foreign key/check)
约束(constraint):对创建的表的列属性.字段进行的限制. 诸如:not null/unique/primary key/foreign key/check 作用范围: ①列级 ...
- 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 ...
- [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 ...
- 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' ...
- 外键 Foreign keys
https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?vi ...
随机推荐
- 构建自己的PHP框架--实现Model类(2)
在上一篇博客中我们简单实现了findOne方法,但我们可以看到,还是有一些问题的,下面我们来修正一下这些问题. 首先是返回的数据中,数字被转换成了字符串.我们需要的是数字啊... PDO中有属性可以支 ...
- spring源码分析之spring-core总结篇
1.spring-core概览 spring-core是spring框架的基石,它为spring框架提供了基础的支持. spring-core从源码上看,分为6个package,分别是asm,cgli ...
- 项目中遇到的Integer问题--转
Integer类型值相等或不等分析 http://www.cnblogs.com/zzllx/p/5778470.html 看到博客园一位博友写的面试问题,其中一题是 Integer a = 1; I ...
- 记录下帮助一位网友解决的关于android子控件的onTouch或onClick和父OnTouch 冲突的问题。
前三天收到位网友的私信求助,问题大概如标题所示.具体是下面的情况,个人感觉,这个问题挺有趣,也会在实际项目开发中很常见.不想看前奏的请直接跳至解决方法. 问题原型: 父控件是自定义的 LinearLa ...
- Android之debug---menu的getActionView()return null
MainActivity代码 @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this a ...
- [翻译]利用顶点位移的VR畸变校正
文章英文原网址: http://www.gamasutra.com/blogs/BrianKehrer/20160125/264161/VR_Distortion_Correction_using_V ...
- 《selenium2 Java 自动化测试实战(第二版)》 更新2016.5.3
java 版来了!! 本文档在<selenium2 Python 自动化测试实战>的基础上,将代码与实例替换为java ,当然,部分章节有变更.这主要更语言本身的特点有关.集合和java下 ...
- STL模板中的map的使用与例题
最近的计分赛,记得自己的都只是过了两题.遇到了两次map,自己在寒假看了一点的map,只知道在字符串匹配的时候可以用的到.但是自己对map的使用还是不够熟练使用,这回在第一次和第二次的计分赛中都遇到可 ...
- WebGIS中一种根据网格索引判断点面关系的方法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 判断点面关系的算法有很多,在我之前的博文中有一篇专门对其进行 ...
- ajax+php+js实现异步刷新表单验证
创建ajax对象 //创建对象 function createAjax(){ var request =false; //IE浏览器,window对象存在ActiveXObject属性 if(wind ...