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','***');
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 ***
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
MYSQL中delete多表關聯刪除資料
delete刪除多表資料,怎樣才能同時刪除多個關聯表的資料呢?這裡做了深入的解釋 1 delete from t1 where 條件 2 delete t1 from t1 where 條件 3 delete t1 from t1,t2 where 條件 4 delete t1,t2 from t1...
oracle刪除資料
oracle在表中刪除資料的語法是 語法結構 delete from表名 where 條件 演示 sql delete from infos where stuid s100103 1 row deleted sql commit truncate 在資料庫操作中,truncate命令 是乙個ddl...
oracle多表關聯刪除資料表記錄方法
oracle多表關聯刪除的兩種方法 第一種使用exists方法 delete from tablea where exits select 1 from tableb where tablea.id tableb.id 第二種使用匿名表方式進行刪除delete from select 1 from ...