Oracle中主從多表刪除數據

Oracle中主從多表刪除數據時,必須用級聯刪除嗎?
一個主表,帶了三個從表,一一關聯,A爲主表
A->B->C->D
從A表中刪除一條數據時,要把BCD表裏相關的數據都刪除的話

用什麼方法最好,必須用級聯刪除嗎?

--------------

1.用觸發器;

2.建表時加關鍵字。比如B表某列關聯A表主鍵列,則:
create table b (col number references a(col) on delete cascade);
後面的C表D表類似處理。

分別在B C D創建外鍵時,都加上on delete cascade屬性

-------------

級聯刪除我沒有用過,象你這種問題我覺得應該用一個存儲過程處理
而存儲過程中要用事務處理,要麼全部刪除成功,要麼什麼都不刪除
先刪除子表,然後再刪除主表

存儲過程要帶一個參數,參數爲主表的主鍵

---------------



--做一個例子測試一下:


SQL> create table table3(id number(8) primary key,name varchar2(8));


Table created


SQL> insert into table3 values(1,'張三');


1 row inserted


SQL> insert into table3 values(2,'李四');


1 row inserted


SQL> commit;


Commit complete


SQL> create table table4(id2 number(6) primary key,id number(8),score number(8),
  2  constraint FK_id foreign key (id) references table3(id));


Table created


SQL> insert into table4 values(10,1,3);


1 row inserted


SQL> insert into table4 values(11,2,2008);


1 row inserted


SQL> commit;


Commit complete


SQL> alter table table3 drop primary key cascade;


Table altered


SQL> select * from table3;


       ID NAME
--------- --------
        1 張三
        2 李四


SQL> select * from table4;


    ID2        ID     SCORE
------- --------- ---------
     10         1         3
     11         2      2008


SQL> delete from table3 where id=1;


1 row deleted


SQL> commit;


Commit complete


SQL> select * from table3;


       ID NAME
--------- --------
        2 李四


SQL> select * from table4;


    ID2        ID     SCORE
------- --------- ---------
     10         1         3
     11         2      2008


SQL>

--從上述可以看出:刪除table3的主關鍵字id=1時,cascade並不能刪除table4表中的id=1的記錄。
--所以希望樓主還是使用觸發器的聯級觸發吧!不過調試時要思路清晰,多做測試。
 --------------------------------------------

級聯刪除既能用外鍵on delete cascade, 也能用觸發器
但是觸發器消耗資源較多, 而且有經驗的DBA可能會知道,解發器會引起許多麻煩, 不建議使用

外鍵多級級聯刪除我測試是完全可以做到的:


SQL> create table t1
  2  (
  3  id varchar2(10),
  4  name varchar2(10)
  5  );
 
Table created
 
SQL> create table t2
  2  (
  3  id varchar2(10),
  4  pid varchar2(10),
  5  name varchar2(10)
  6  );
 
Table created
 
SQL> 
SQL> create table t3
  2  (
  3  id varchar2(10),
  4  pid varchar2(10),
  5  name varchar2(10)
  6  );
 
Table created


SQL> alter table t1
  2     add constraint pk_t1 primary key (id);
 
Table altered


SQL> 
SQL> alter table t2
  2     add constraint pk_t2 primary key (id);
 
Table altered
 
SQL> 
SQL> alter table t3
  2     add constraint pk_t3 primary key (id);
 
Table altered


SQL> alter table t2 add constraint FK_t1_t2 foreign key (pid) references t1(id) on delete cascade not deferrable;
 
Table altered


SQL> alter table t3 add constraint FK_t2_t3 foreign key (pid) references t2(id) on delete cascade not deferrable;
 
Table altered


SQL> insert into t1 values('1','aaa');
 
1 row inserted
 
SQL> insert into t1 values('2','bbb');
 
1 row inserted
 
SQL> insert into t2 values ('1','1','mmm');
 
1 row inserted
 
SQL> insert into t2 values ('2','1','nnn');
 
1 row inserted
 
SQL> insert into t3 values ('1','1','xxx');
 
1 row inserted
 
SQL> commit;
 
Commit complete
 
SQL> select * from t1;
 
ID         NAME
---------- ----------
1          aaa
2          bbb
 
SQL> select * from t2;
 
ID         PID        NAME
---------- ---------- ----------
1          1          mmm
2          1          nnn
 
SQL> select * from t3;
 
ID         PID        NAME
---------- ---------- ----------
1          1          xxx


SQL> delete t1 where id = '1';
 
1 row deleted
 
SQL> commit;
 
Commit complete
 
SQL> select * from t1;
 
ID         NAME
---------- ----------
2          bbb
 
SQL> select * from t2;
 
ID         PID        NAME
---------- ---------- ----------
 
SQL> select * from t3;
 
ID         PID        NAME
---------- ---------- ----------
 
SQL> rollback;
 
Rollback complete

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章