Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 22D6E632F45 for ; Sat, 13 Feb 2010 18:46:18 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 65502-01 for ; Sat, 13 Feb 2010 22:45:53 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 237D2632820 for ; Sat, 13 Feb 2010 18:46:07 -0400 (AST) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1NgQkV-0004KR-I3 for pgsql-sql@postgresql.org; Sat, 13 Feb 2010 23:46:03 +0100 Received: from e177120010.adsl.alicedsl.de ([85.177.120.10]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 13 Feb 2010 23:46:03 +0100 Received: from tim by e177120010.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 13 Feb 2010 23:46:03 +0100 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: pgsql-sql@postgresql.org To: pgsql-sql@postgresql.org From: Tim Landscheidt Subject: Re: Can i force deletion of dependent rows? Date: Sat, 13 Feb 2010 22:45:41 +0000 Organization: Lines: 35 Message-ID: References: <6caee9521002120424l27ec8d3eq3b69522d0a773686@mail.gmail.com> <6caee9521002122211q7e4fd2fcj1a0489fd64dc204a@mail.gmail.com> <6caee9521002130925u46ac27c1u9c0c2f3f202e411f@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: e177120010.adsl.alicedsl.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:b41FqO9mwEllTkk7qDZNv545O7w= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=AWL=0.000, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/55 X-Sequence-Number: 33654 Shruthi A wrote: > Thanks people, but the trigger doesnt really solve my problem. You see, > there is actually a CHAIN of dependencies where B references A, C references > B, D and E reference C and so on... So if i start writing triggers for all > these, there'll be a huge process to follow. I'm not strictly against it, i > might use it as a last resort, but i'm asking if a simple DML statement > would solve my problem. > Dear Shoaib, I did mention in a note that I don't want to recreate the 2 > tables with the 'on delete cascade' clause. The tables are ALREADY created > and populated. And they are huge. So i cannot afford to drop and recreate > them. I want an option in DML only. If that is possible that is. You don't have to drop and recreate them, you could just temporarily change the foreign key definition (untested): | BEGIN WORK; | ALTER TABLE B DROP CONSTRAINT B_z_FKey; | ALTER TABLE B ADD CONSTRAINT B_z_FKey FOREIGN KEY(z) REFERENCES A(x) ON DELETE CASCADE; | DELETE FROM A WHERE x = 'your x'; | ALTER TABLE B DROP CONSTRAINT B_z_FKey; | ALTER TABLE B ADD CONSTRAINT B_z_FKey FOREIGN KEY(z) REFERENCES A(x) ON DELETE NO ACTION; | COMMIT WORK; But obviously this is no better than a simple "DELETE FROM B WHERE z = 'your x';" as you still have to name the dependen- cy chain yourself. You can query the information_schema to build this chain (e. g. cf. ), but I would invest more time in rethinking your processes. Tim