Re: Are JOINs allowed with DELETE FROM

From: John Arbash Meinel <john(at)arbash-meinel(dot)com>
To: Gaetano Mendola <mendola(at)bigfoot(dot)com>
Cc: Steven Rosenstein <srosenst(at)us(dot)ibm(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Are JOINs allowed with DELETE FROM
Date: 2005-02-06 17:58:45
Message-ID: 42065AD5.50001@arbash-meinel.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Gaetano Mendola wrote:

> Steven Rosenstein wrote:
>
>> DELETE FROM detail JOIN summary ON (summary.id=detail.sum_id) WHERE
>> collect_date='2005-02-05';
>
>
You have to tell it what table you are deleting from. Select * from A
join B is both tables. What you want to do is fix the where clause.

> DELETE FROM detail WHERE detail.sum_id in ( select id from summary )
> AND collect_date='2005-02-05';
>
I'm guessing this should actually be
DELETE FROM detail WHERE detail.sum_id in ( SELECT id FROM summary WHERE
collect_date='2005-02-05' );
Otherwise you wouldn't really need the join.

You have to come up with a plan that yields rows that are in the table
you want to delete. The rows that result from
select * from detail join summary, contain values from both tables.

If you want to delete from both tables, I think this has to be 2
deletes. Probably best to be in a transaction.

BEGIN;
DELETE FROM detail WHERE ...
DELETE FROM summary WHERE collect_date = '2005-02-05';
COMMIT;

>
> Regards
> Gaetano Mendola
>
John
=:->

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Steven Rosenstein 2005-02-06 19:33:16 Re: Are JOINs allowed with DELETE FROM
Previous Message Michael Fuhr 2005-02-06 17:50:29 Re: Are JOINs allowed with DELETE FROM