X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 3DC4F9DCBC3 for ; Wed, 8 Feb 2006 13:32:33 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 98227-04 for ; Wed, 8 Feb 2006 13:32:32 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 2C3699DC821 for ; Wed, 8 Feb 2006 13:32:31 -0400 (AST) Received: from nelson.osl.com (216-21-133-120.ip.van.radiant.net [216.21.133.120]) by svr4.postgresql.org (Postfix) with ESMTP id 4C4705AF06E for ; Wed, 8 Feb 2006 17:32:31 +0000 (GMT) Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0 Subject: Re: Delete with join -- deleting related table entries? Date: Wed, 8 Feb 2006 09:32:27 -0800 Message-ID: <144D12D7DD4EC04F99241498BB4EEDCC20CC56@nelson.osl.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] Delete with join -- deleting related table entries? Thread-Index: AcYs0yTjwft69upxR4aOmQji7ewuHQAAcGLQ From: "Owen Jacobson" To: "Bryce Nesbitt" , X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.096 required=5 tests=[AWL=0.096] X-Spam-Score: 0.096 X-Spam-Level: X-Archive-Number: 200602/74 X-Sequence-Number: 24084 Bryce Nesbitt wrote: > When I delete a record from a certain table, I need to delete a > (possibly) attached note as well. How can I do this with=20 > postgres? The > tables are like this: >=20 > reservation > reservation_id > stuff... >=20 > isuse > issue_id > reservation_id references reservation (reservation_id) -- ADD > stuff.. >=20 > note > issue_id references isuse (issue_id) -- ADD (kept typo in example) > text comments... >=20 > A select that pulls out what I want to delete is: >=20 > SELECT reservation_id,issue_id,note_id,eg_note.comments FROM > eg_reservation > LEFT JOIN eg_issue USING (reservation_id) > LEFT JOIN eg_note USING (issue_id) > WHERE reservation_id > condition; >=20 > Can anyone help me turn this into a DELETE statement? 1. Add foreign key references between the tables to ensure that there = are only notes and issues (isuses? :) for existing issues and = reservations respectively. You can make those references 'ON DELETE = CASCADE' so that a delete of the original reservation cascades down to = related entries in the issue table, which in turn cascade down to the = related entries in the note table. 2. Or... BEGIN; DELETE FROM note WHERE issue_id IN (SELECT issue_id FROM isuse WHERE reservation_id =3D reservation_to_delete); DELETE FROM isuse WHERE reservation_id =3D reservation_to_delete; DELETE FROM reservations WHERE reservation_id =3D = reservation_to_delete; END; with an appropriate value or expression substituted into = reservation_to_delete. This would be the "hard way", but (as it's in a = single transaction) will still protect other clients from seeing a = partial delete. Get yourself a good, non-MySQL-specific database book, which should = explain how referential integrity is handled in databases. -Owen