共計 3667 個字符,預計需要花費 10 分鐘才能閱讀完成。
這篇文章給大家介紹執行 truncate 觸發 ORA-02266 解決過程是怎樣的,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
開發提了個需求,要求將測試數據庫的數據清空,其中涉及主子表的關系,如下所示,
最直觀的做法,就是 truncate 表。首先 truncate 各個子表,但是當執行 truncate 主表的時候,提示錯誤,ORA-02266: unique/primary keys in table referenced by enabled foreign keys。
子表此時沒數據,為何不讓刪除主表的數據?
我們模擬下過程,首先創建測試表,主表 a_1,子表 b_1,
SQL create table a_1 (id number);
Table created.
SQL create table b_1 (id number, id_a_1 number);
Table created.
SQL alter table a_1 add constraint pk_a_1 primary key(id);
Table altered.
SQL alter table b_1 add constraint fk_b_a foreign key (id_a_1) references a_1 (id);
Table altered.
此時 truncate 子表和主表,均會成功,
SQL truncate table b_01;
Table truncated.
SQL truncate table a_01;
Table truncated.
但是,當主子表有數據,truncate 子表,再做 truncate 主表的操作,就會提示 ORA-02266 的錯誤,
SQL insert into a_1 values(1);
1 row created.
SQL insert into b_1 values(1,1);
1 row created.
SQL commit;
Commit complete.
SQL truncate table b_1;
Table truncated.
SQL truncate table a_1;
truncate table a_1
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
ORA-02262 的錯誤含義是,“表中的唯一 / 主鍵被啟用的外鍵引用”,
02262, 00000, ORA-%s occurs while type-checking column default value expression
// *Cause: New column datatype causes type-checking error for existing column
// default value expression.
// *Action: Remove the default value expression or don t alter the column
// datatype.
但是子表已經沒數據了,主表怎么還會提示這個錯誤?究其原因,這和 truncate 操作有關,因為 truncate 是 DDL,但是 DDL 語句不會檢查約束,換句話說,他不知道子表有沒有數據依賴于他,索性不讓做了。
但是有個疑問,當主子表無數據,此時 truncate 主表,是可以成功的,只有當主子表有數據的時候,truncate 主表才會提示。我看了下統計信息中,表中無數量記錄,Oracle 是如何知道當前表有數據,禁止 truncate?
我猜測,可能是延遲段的影響?
1. 看下表中有數據,執行 truncate 產生的 10046,其中 truncate table a_1 主表時,有個綁定變量的參數是 B_1,推測由此知道 a_1 有外鍵引用,進而報錯,err=2266
…
LOCK TABLE A_1 IN EXCLUSIVE MODE NOWAIT
…
truncate table a_1
…
Bind#1
oacdty=01 mxl=32(03) mxlc=00 mal=00 scl=00 pre=00
oacflg=10 fl2=0001 frm=01 csi=873 siz=0 off=24
kxsbbbfp=7f2df926afc8 bln=32 avl=03 flg=01
value= B_1
…
ERROR #139835430202688:err=2266 tim=1562853681567125
…
2. 看下表中無數據,執行 truncate 產生的 10046,發現他會檢索 deferred_stg$ 視圖,truncate 是靠 aw_trunc_proc 存儲過程
…
LOCK TABLE A_1 IN EXCLUSIVE MODE NOWAIT
…
select pctfree_stg, pctused_stg, size_stg,initial_stg, next_stg, minext_stg, maxext_stg, maxsiz_stg, lobret_stg,mintim_stg, pctinc_stg, initra_stg, maxtra_stg, optimal_stg, maxins_stg,frlins_stg, flags_stg, bfp_stg, enc_stg, cmpflag_stg, cmplvl_stg from deferred_stg$ where obj# =:1
…
truncate table a_1
…
BEGIN
aw_trunc_proc(ora_dict_obj_type, ora_dict_obj_name, ora_dict_obj_owner);
END;
…
3. 關閉 session 級別的延遲段特性
gment_creation=false;
Session altered.
表中無數據,執行 truncate 產生的 10046,和上面兩個比,操作最簡單,LOCK 表,執行 truncate,沒其他操作了
…
LOCK TABLE A_1 IN EXCLUSIVE MODE NOWAIT
…
truncate table a_1
…
從現象看,不是延遲段特性,導致兩者的區別,需要請大佬指教。
針對 ORA-02266 的錯誤,有幾種解決方案,
方案 1:禁用約束 -truncate- 啟用約束
可以參考 MOS 這篇文章《OERR: ORA-2266 unique/primary keys in table referenced by enabled foreign keys Reference Note (Doc ID 19499.1)》。
1. 找出主表的約束
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
或者執行下列任意一個 SQL,都可以得到主鍵相關的外鍵參考
select a.constraint_type,a.table_name,a.status, b.table_name,b.column_name,b.constraint_name from user_constraints a
inner join user_cons_columns b on a.constraint_name = b.constraint_name
where a.r_constraint_name= 主鍵約束名稱
select c.TABLE_NAME
from all_constraints p, all_constraints c
where p.table_name = 主鍵約束名稱
and p.OWNER = SYS_CONTEXT(USERENV , CURRENT_SCHEMA)
and c.OWNER=SYS_CONTEXT(USERENV , CURRENT_SCHEMA)
and c.constraint_type = R
and p.CONSTRAINT_NAME = c.R_CONSTRAINT_NAME;
2. 刪除約束
SQL alter table a_1 disable primary key cascade;
Table altered.
3. 執行 truncate
4. 啟用約束
只是需要注意,enable 恢復主鍵的操作,并不會自動 enable 外鍵,需要手工 enable 外鍵
SQL alter table tbl_a enable primary key;
Table altered.
SQL alter table tbl_b enable constraint fk_b_a;
Table altered.
方案 2:delete 刪除
使用 delete,DML 操作是可以正常刪除主表,只是不適合數據量很大的場景。
關于執行 truncate 觸發 ORA-02266 解決過程是怎樣的就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。