dbTalk Databases Forums  

Re: Newbie: delete from multiple tables?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Re: Newbie: delete from multiple tables? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ian McCall
 
Posts: n/a

Default Re: Newbie: delete from multiple tables? - 07-17-2003 , 09:25 AM







"abhijit" <abhi (AT) nomail (DOT) com> wrote

Quote:
delete table_1
where table_1.field1 in (select table_2.field1 from table_2 )and
table_1.field2 in (select table_2.field2 from table_2)
Won't that potentially delete rows where one field matches but not the
other?

So...

table_1:
field1: 1
field2: 2

field1: 3
field2: 4

And table_2:
field1: 1
field2: 7

field1: 9
field2: 2

I -believe- (happy to be proved wrong...) that your statement above would
match a row as table_1's field1 is in (1, 9) and table_1's field2 is in (7,
2). However, no row -should- be matched as no row in table_2 has a fields
identical to those in table_1.

That not the case?


Cheers,
Ian




Reply With Quote
  #2  
Old   
Niall Litchfield
 
Posts: n/a

Default Re: Newbie: delete from multiple tables? - 07-17-2003 , 10:16 AM






"Ian McCall" <ian (AT) eruvia (DOT) org> wrote

Quote:
Hello.

I'm moving over from Sybase, and am looking for the equivalent syntax to
the
following command:


delete table_1
from table_1, table_2
where table_1.field1 = table_2.field1
and table_1.field2 = table_2.field2

In other words delete, only from table_1, anything where field1 and field2
match a row in table_2.

Any ideas?
SQL> create table t1 (id number primary key,col2 number);

Table created.

SQL> create table t2 (id number primary key,col2 number);

Table created.

SQL> insert into t1 values (1,1);

1 row created.

SQL> insert into t1 values (2,1);

1 row created.

SQL> insert into t1 values (2,2);
insert into t1 values (2,2)
*
ERROR at line 1:
ORA-00001: unique constraint (NIALL.SYS_C0025839) violated


SQL> insert into t1 values (3,2);

1 row created.

SQL> insert into t2 values(1,1);

1 row created.

SQL> insert into t2 values(3,2);

1 row created.

SQL> commit;

Commit complete.

SQL> delete t1
2 where exists (
3 select null from t2
4 where t1.id=t2.id
5 and t1.col2=t2.col2);

2 rows deleted.

SQL> select * from t1;

ID COL2
---------- ----------
2 1

SQL> spoo off






Reply With Quote
  #3  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Newbie: delete from multiple tables? - 07-17-2003 , 10:23 AM



Niall Litchfield wrote:
Quote:
SQL> delete t1
2 where exists (
3 select null from t2
4 where t1.id=t2.id
5 and t1.col2=t2.col2);

I was just about to post an answer with that version of the delete
statement as well :-)

I would think that this is more efficient then having two subselects.

Thomas



Reply With Quote
  #4  
Old   
Niall Litchfield
 
Posts: n/a

Default Re: Newbie: delete from multiple tables? - 07-17-2003 , 03:20 PM



"Thomas Kellerer" <spam_eater (AT) gmx (DOT) net> wrote

Quote:
Niall Litchfield wrote:

SQL> delete t1
2 where exists (
3 select null from t2
4 where t1.id=t2.id
5 and t1.col2=t2.col2);


I was just about to post an answer with that version of the delete
statement as well :-)

I would think that this is more efficient then having two subselects.
I *believe* that deleting from a view would be yet more efficient, but
implies that the base tables need primary keys.


--
Niall Litchfield
Oracle DBA
Audit Commission UK
*****************************************
Please include version and platform
and SQL where applicable
It makes life easier and increases the
likelihood of a good answer
******************************************




Reply With Quote
  #5  
Old   
JJ Reynolds
 
Posts: n/a

Default Re: Newbie: delete from multiple tables? - 07-18-2003 , 12:41 PM



or.. an even cuter syntax!

delete from t1 where (col1, col2) in (select col1, col2 from t2);


"Niall Litchfield" <niall.litchfield (AT) dial (DOT) pipex.com> wrote

Quote:
"Thomas Kellerer" <spam_eater (AT) gmx (DOT) net> wrote in message
news:bf6f1v$beoqf$1 (AT) ID-13919 (DOT) news.uni-berlin.de...
Niall Litchfield wrote:

SQL> delete t1
2 where exists (
3 select null from t2
4 where t1.id=t2.id
5 and t1.col2=t2.col2);


I was just about to post an answer with that version of the delete
statement as well :-)

I would think that this is more efficient then having two subselects.

I *believe* that deleting from a view would be yet more efficient, but
implies that the base tables need primary keys.


--
Niall Litchfield
Oracle DBA
Audit Commission UK
*****************************************
Please include version and platform
and SQL where applicable
It makes life easier and increases the
likelihood of a good answer
******************************************





Reply With Quote
  #6  
Old   
JJ Reynolds
 
Posts: n/a

Default Re: Newbie: delete from multiple tables? - 07-18-2003 , 01:25 PM



It's recent.. I know it works in 9i, but not sure about 8i (don't have an 8i
instance to try it on). It absolutely did not work in 7.x.





"Thomas Kellerer" <spam_eater (AT) gmx (DOT) net> wrote

Quote:
JJ Reynolds schrieb:
or.. an even cuter syntax!

delete from t1 where (col1, col2) in (select col1, col2 from t2);


This is cool

I didn't know that this is possible.
After so many years of SQL experience one can still learn new things..

Cheers
Thomas




Reply With Quote
  #7  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Newbie: delete from multiple tables? - 07-18-2003 , 02:41 PM



I just tried it with Oracle 8.1.7 and PostgreSQL 7.2.1
It works with both...

Cheers
Thomas

JJ Reynolds schrieb:

Quote:
It's recent.. I know it works in 9i, but not sure about 8i (don't have an 8i
instance to try it on). It absolutely did not work in 7.x.



"Thomas Kellerer" <spam_eater (AT) gmx (DOT) net> wrote in message
news:bf9cia$8rs$1 (AT) svr8 (DOT) m-online.net...

JJ Reynolds schrieb:

or.. an even cuter syntax!

delete from t1 where (col1, col2) in (select col1, col2 from t2);



This is cool

I didn't know that this is possible.
After so many years of SQL experience one can still learn new things..

Cheers
Thomas




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.