阿木伯 著 |
|
刪除a表中和b表相同的數(shù)據(jù) |
|
- 軟件環(huán)境:
1、Windows NT4.0+ORACLE 8.0.4
2、ORACLE安裝路徑為:C:\ORANT
- 問題提出:
1、在做數(shù)據(jù)轉(zhuǎn)儲業(yè)務的時候,如果發(fā)生操作錯誤,有可能出現(xiàn)主表和副表中都有同一種數(shù)據(jù),
這樣結(jié)算的結(jié)果就有可能發(fā)生錯誤。
- 實現(xiàn)方法:
SQL> create table a (
2 bm char(4), --編碼
3 mc varchar2(20) --名稱
4 )
5 /
表已建立.
SQL> insert into a values('1111','1111');
SQL> insert into a values('1112','1111');
SQL> insert into a values('1113','1111');
SQL> insert into a values('1114','1111');
SQL> insert into a values('1115','1111');
SQL> create table b as select * from a where 1=2;
表已建立.
SQL> insert into b values('1111','1111');
SQL> insert into b values('1112','1111');
SQL> insert into b values('1113','1111');
SQL> insert into b values('1114','1111');
SQL> commit;
完全提交.
SQL> select * from a;
BM MC
---- --------------------
1111 1111
1112 1111
1113 1111
1114 1111
1115 1111
SQL> select * from b;
BM MC
---- --------------------
1111 1111
1112 1111
1113 1111
1114 1111
方法一:exists子句
SQL> delete from a where exists (select 'X' from b where a.bm=b.bm and a.mc=b.mc);
刪除4個記錄.
where條件:如果兩個表中都擁有相同字段的主鍵(primary key),則只需比較兩個主鍵就可以了
方法二:in子句
SQL> delete from a where (bm,mc) in (select bm,mc from b);
刪除4個記錄.
SQL> select * from a;
BM MC
---- --------------------
1115 1111
實際測試結(jié)論:
在表不是很大時,用in子句速度還可以忍受,而如果記錄量很多時(十萬條以上),in子句簡直讓人難以人忍受,速度奇慢。
|
【最后更新:】 |
|