Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWKK4-0004PH-BN for pgsql-novice@arkaria.postgresql.org; Tue, 13 Apr 2021 14:47:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lWKK3-0005J4-AE for pgsql-novice@arkaria.postgresql.org; Tue, 13 Apr 2021 14:47:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWKK3-0005Ix-4D for pgsql-novice@lists.postgresql.org; Tue, 13 Apr 2021 14:47:23 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWKK1-0004kP-50 for pgsql-novice@postgresql.org; Tue, 13 Apr 2021 14:47:22 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 13DElImZ3190281; Tue, 13 Apr 2021 10:47:18 -0400 From: Tom Lane To: steve@tusol.co.uk cc: PostGreSQL Subject: Re: Advice on foreign key and cascading delete design - postgresql 12.6 In-reply-to: References: Comments: In-reply-to "Steve Tucknott (TuSol)" message dated "Tue, 13 Apr 2021 13:40:51 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3190279.1618325238.1@sss.pgh.pa.us> Date: Tue, 13 Apr 2021 10:47:18 -0400 Message-ID: <3190280.1618325238@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Steve Tucknott (TuSol)" writes: > I have a few tables that are subservient to multiple other tables > (notes, addresses, attached documents etc). > These tables carry the 'owning' table name and its record's PK. > I can manually keep these tables tidy and avoid orphans, but is there a > way within postgresql to reference these tables on a FK constraint to > automatically delete the subservient recs when the parent is deleted? Maybe I'm missing something, but aren't you just looking for the ON DELETE CASCADE option of foreign key constraints? regards, tom lane