ORA-02266错误解决方案

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> ORA-02266错误解决方案

预计阅读时间:10分钟

ORA-02266错误解决方案

世界杯战报:

2018年世界杯揭幕战在莫斯科卢日尼基体育场举行,东道主俄罗斯迎战沙特阿拉伯。上半场戈洛温助攻加辛斯基率先破门,扎戈耶夫拉伤退场,替补登场的切里舍夫为俄罗斯扩大比分;下半场戈洛温助攻替补上阵仅一分钟的久巴头球破门,久巴补时阶段助攻切里舍夫打入一脚世界波,戈洛温比赛读秒阶段打入压哨任意球世界波。最终,俄罗斯5-0战胜沙特阿拉伯,取得本届世界杯开门红。

ORA-02266错误解决方案
  1. 这是自1986年墨西哥世界杯的扎瓦罗夫后,俄罗斯国家队(包含前苏联)首次有球员能够在世界杯单场比赛送出至少两次助攻。莫斯科中央陆军中场戈洛温震惊了世界,他打进1球,并送出2次助攻,豪门在向他招手。

  2. 替补上场的切里舍夫在第42分钟打入一球,他也成为世界杯历史上第一个在揭幕战进球的替补球员。

  3. 很为这位已经看好海景房和宾利的朋友感到可惜,是有5个进球,就是方向错了,并且少一个进球,

ORA-02266错误解决方案

开始我们的话题之前,先说个网友回复,文章《》中对于Windows下进程的kill,网友“何所冬暖,何所夏凉”指出了另两种kill的方法,

  1. windows有个taskkill /pid pid可以进行杀进程
  2. 还有种是直接打开任务管理器,点性能,资源管理器,CPU,搜索pid,进程名,文件名均可搜索到对应的进程,右击进行结束进程!

下面是这两种方法的实验。

1. windows有个taskkill /pid pid可以进行杀进程

方法1:

  1. netstat检索进程PID
ORA-02266错误解决方案
  1. 根据PID号,taskkill干掉进程
ORA-02266错误解决方案

方法2:

  1. netstat检索进程PID
ORA-02266错误解决方案
  1. tasklist检索进程名称
ORA-02266错误解决方案
  1. 根据进程名称,taskkill干掉进程
ORA-02266错误解决方案

2. 直接打开任务管理器,点性能,资源管理器(此处应为“资源监视器”),CPU,搜索pid,进程名,文件名均可搜索到对应的进程,右击进行结束进程!

谢谢“何所冬暖,何所夏凉”朋友的建议。

开始我们今天的话题。

同事反馈,一张主表,一张子表,有主外键关联,使用truncate,先删除子表,再删除主表时,提示ORA-02266: unique/primary keys in table referenced by enabled foreign keys错误。我们做下实验。

  1. 创建测试表,TBL_A是主表,TBL_B是子表,有主外键关系,

SQL create table tbl_a (id number, name varchar2(1));

Table created.

SQL create table tbl_b (id number, id_a number, name varchar2(1));

Table created.

SQL alter table tbl_a add constraint pk_a primary key(id);

Table altered.

SQL alter table tbl_b add constraint fk_b_a foreign key(id_a) references tbl_a(id);

Table altered.
  1. 插入测试数据,

SQL insert into tbl_a values(1, 'a');

1 row created.

SQL insert into tbl_b values(1, 1, 'a');

1 row created.

SQL commit;

Commit complete.

SQL select * from tbl_a;

        ID N
---------- -
         1 a

SQL select * from tbl_b;

        ID       ID_A N
---------- ---------- -
         1          1 a
  1. truncate删除子表,

SQL truncate table tbl_b;

Table truncated.
  1. truncate删除主表,提示ORA-02266错误,

SQL truncate table tbl_a;
truncate table tbl_a
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys

关于ORA-02266介绍,

02266, 00000, “unique/primary keys in table referenced by enabled foreign keys”// *Cause: An attempt was made to truncate a table with unique or //         primary keys referenced by foreign keys enabled in another table.//         Other operations not allowed are dropping/truncating a partition of a//         partitioned table or an ALTER TABLE EXCHANGE PARTITION.// *Action: Before performing the above operations the table, disable the //          foreign key constraints in other tables. You can see what //          constraints are referencing a table by issuing the following //          command://          SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = “tabnam”;

这是因为truncate是DDL语句,无法确认约束的存在,

because truncate isn’t going to verify the constraint, truncate is ddl.

MOS这篇文章《OERR: ORA-2266 “unique/primary keys in table referenced by enabled foreign keys” Reference Note (Doc ID 19499.1)》给出了解决方案,

  1. 找出主表的约束

To find out what other table(s) are referencing the one you are trying to drop use:


SELECT constraint_name
          FROM user_constraints
         WHERE table_name = 'table_you_are_trying_to_drop'
           AND constraint_type = 'P';
        
        SELECT *
          FROM user_constraints
         WHERE constraint_type = 'R'
           AND r_constraint_name = 'constraint_name_returned_above';

The above queries assume that the foreign key constraint is referencing a primary key constraint. If the first query returns no rows, change the ‘P’ in the ‘where’ clause to ‘U’ as it is likely that a unique constraint is being referenced instead. This is rare, however, and generally regarded as bad design.)

  1. 删除约束

You must DROP the constraints - disabling them is not sufficient.

上述操作,指出禁用不足够,需要删除,其实未必,如下所示,我们使用cascade禁用主键约束,此时主表主键,以及子表外键,约束状态DISABLED,当前我们没有删除约束,但是能支持truncate操作,


SQL alter table tbl_a disable primary key cascade;

Table altered.

SQL SELECT constraint_name, constraint_type, status FROM user_constraints WHERE table_name IN ('TBL_A','TBL_B');

CONSTRAINT_NAME                C STATUS
------------------------------ - --------
PK_A                           P DISABLED
FK_B_A                         R DISABLED

SQL truncate table tbl_b;

Table truncated.

SQL truncate table tbl_a;

Table truncated.

只是需要注意,enable恢复主键的操作,并不会自动enable外键,


SQL alter table tbl_a enable primary key;

Table altered.

SQL SELECT constraint_name, constraint_type, status FROM user_constraints WHERE table_name IN ('TBL_A','TBL_B');

CONSTRAINT_NAME                C STATUS
------------------------------ - --------
PK_A                           P ENABLED
FK_B_A                         R DISABLED

需要手工enable外键,


SQL alter table tbl_b enable constraint fk_b_a;

Table altered.

SQL SELECT constraint_name, constraint_type, status FROM user_constraints WHERE table_name IN ('TBL_A','TBL_B');

CONSTRAINT_NAME                C STATUS
------------------------------ - --------
PK_A                           P ENABLED
FK_B_A                         R ENABLED

针对这种主外键,另一种删除方法,就是使用DELETE这种DML,按“子表”-“主表”顺序操作即可,当然他和truncate相比,效率上明显不同,针对不同场景,选择不同的方案,


SQL delete from tbl_b;

1 rows deleted.

SQL delete from tbl_a;

1 row deleted.

关于ORA-02266这个错误,有些短小精悍的文章,可以参考,

《ORA-2266 when Dropping Parent Partition of a Reference Partitioned Table (Doc ID 2008493.1)》

《ORA-2266 When Partition Referenced by FK Constraint and Partition Dropped (Doc ID 1469877.1)》

如果您觉得本文有帮助,欢迎关注转发:bisal的个人杂货铺,

ORA-02266错误解决方案
本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> ORA-02266错误解决方案


 上一篇
数据库之MySQL(一) 数据库之MySQL(一)
点击上方”python宝典”,关注获取python全套视频, 技术文章第一时间送达! 概述1、什么是数据库 ?   数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2、什么是 MySQL、Oracle、SQLite
下一篇 
数据库之MySQL(二) 数据库之MySQL(二)
点击上方”python宝典”,关注获取python全套视频, 技术文章第一时间送达! 数据表基本1、创建表 create table 表名( 列名 类型 是否可以为空, 列名 类型 是否可以为空 )ENGINE=I