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 1lWLaw-0007e3-Tg for pgsql-novice@arkaria.postgresql.org; Tue, 13 Apr 2021 16:08:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lWLav-0004zr-Rj for pgsql-novice@arkaria.postgresql.org; Tue, 13 Apr 2021 16:08:53 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWLav-0004zj-Kd for pgsql-novice@lists.postgresql.org; Tue, 13 Apr 2021 16:08:53 +0000 Received: from mout.gmx.net ([212.227.15.18]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWLao-0006PJ-Vr for pgsql-novice@lists.postgresql.org; Tue, 13 Apr 2021 16:08:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1618330124; bh=EQ144RfUPERnCeFJaRLVzfQa8E7fjRz9mVKbTTbU87g=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=W+T1sZYGinw0XINDdWdNIrbpvu+8yZoToGmT/K3mNKG+CqQcXRxDDAx+Sjpi4mwjy zRvRth9RUlaOImoYhadjVyLKStBZ8kj9zrHBpIKhcTnU6DbuGZt9uVZegjnwfKxt2U zjxp4kYLX27oV0atgQpF1EOWyPA0obuC+WjAOcyE= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from mail.defcon1.lan ([93.15.31.113]) by mail.gmx.net (mrgmx004 [212.227.17.184]) with ESMTPSA (Nemesis) id 1N0X8o-1liGLI0LAK-00wSrx; Tue, 13 Apr 2021 18:08:44 +0200 Date: Tue, 13 Apr 2021 18:08:41 +0200 From: Bzzzz To: pgsql-novice@lists.postgresql.org Cc: steve@tusol.co.uk Subject: Re: Advice on foreign key and cascading delete design - postgresql 12.6 Message-ID: <20210413180841.3ba6e741@msi.defcon1.lan> In-Reply-To: References: <3190280.1618325238@sss.pgh.pa.us> Organization: Anyone, anywhere but in banana demokratik republik of france or UERSS. Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:CuA6eKqZmNZLgF90rkRfiPMh4anXgxyIOxMxrqJkOIanco4h0IC 0ub4g3boxqQXYRrpNELFDr0E4YKrfnL/QcEe6C8NQG3YfoTsucItE8uNubNcpCFTAj/rZRM TdoXUKjN0qMngcc1qUEoNd3sEZjJsCkO7N0LVy4e7mcFom4ARtCelYaXBXox3pJu1qjDutz 3SYL2rh+/sY0QE7Z/+C8w== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:CSEvjrt5k/A=:C60MIdf+5ezGWWVnBEyk46 X7WnfiBZL9/s7xM3RshCPlqQuBFOdR4AZtiBPuA3XPIwdjjcGpPbTGorY9i8oteVRsIBguQi5 ykefXjdgvwpF5VNYkoPMT4PYT+3OyC0d51dienF7dpVP2+5/kQX/xDEI1V5vd9ZyQftiHQu8W LncaTUIzlrVYwOlElO2DvbMcTykCHl2ng7jQuQvpmZn037dLl5RYGCrHIpR9blyDKKEzChiEN mfNc6/vuyUbHSgsrjMtT78qiI3Oe1R+OK0du2ghZtwa82NoORhAssdK6wd5+C1HKC5ddTDy6t JDMmX3GNouKp1Jji3Yi5H7hk2XXAJCFpXOfdzcpvLN8E+245VKZf9zVSZiDeataoWGtU7LCoq PjRKPsyzXzv7fgpDaRYAphX0NnBvuzj08R7wepoeJuzYEYZadtpEdQOllIB3F461R8EiCDZT1 rm3/cUaOTfc+t7OZvO2H6URoOA1DpazwVBPi+4UGP0ThKoIUMy1JhegEN1U03/2TlWWqfrFcV m2d67yZJOCPx1XadVVfnnnkPQcTEae/cnvZP0Hx0Iq/sSOeGfOouVXYa0/f4zKIQObBlcEDBe IebvudQETX0KGhOeYsTjNuuFIzVIXBfo7Hf3jM1MGr5XQ+BKAjgFqw9SytbCvdApnEX7mWp/l Ff705e6kWeaJONZG3YSbaXLuSxi1RX580JZ2hgHlk9w5rgzjBaWIK/sUnLhWWDZuOjSVOp66H 6fLYAbFLWnUCOe+o7Tj+HCftpCzkJUyE8BhLxxPLqEeD0bCsgbZdhvGyVn1lUCdKJogRycL3t BGjzf7OVxWJNFQjTJ/7wQFplS95JTKD2a+hAdyeDLqagyVlQLyx4BuiOxex6M2gnZhj0A2YEb idU2IWRAZ1XgXzGDBhsRnyFOvYaQT8zzV5R8znSdlgsiVzBcKWvc8GSXje+brxW8PLApUOMfV zRH/sQTL1d7WRqQSceZM6tpMZ7oYOjbqMVCYC3zjtpkIjar5UIIkZU63IFUR/4m/b0s5o2xRL qkd0yKJ0TvIF/1zmtkLnOeEnqhuEJ0gZBFtYAsZ2t845ERdzbmJBNrtflfF1j+nvJvDA97caU a/5QLnFyo66OhiLoy92lk8ALafoTHRm2A7xiBasK51p2suojTEDBhUflhAFPa3GNXdfYpw6be QV0nJgNBpGlMhMNATVSvzf7eSu7Huf45dUv8uQYyieZsNkk+2ByFRpHfY696dgrpSvKNg= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 13 Apr 2021 16:48:48 +0100 "Steve Tucknott (TuSol)" wrote: > > Maybe I'm missing something, but aren't you just looking for theON > > DELETE CASCADE option of foreign key constraints? > > regards, tom lane > > Tom, > Maybe it's me that's missing it. > > On my subordinate table I have two 'identification' fields - one > containing a varchar for the table name and the other a value for the > PK for that table. So if I add a 'note' on my notes table for my > suppliers table record 1, I would have an entry on notes with > ...'suppliers', 1, 'some note text'... > . > ..and on my supplier table a record with PK of 1. So supplier PK 1 has > a note of 'some note text'. > > How do I set up the FK on the notes table? All I can see is the option > to link on column names, so I can set up: > ...CONSTRAINT notes_c1 FOREIGN KEY (foreignRecNo) REFERENCES supplier > ... > > BUT that doesn't work as far as I can see, as I may have multiple > foreignrecnos on notes with value 1, each of which is dependent on the > foreigntablename as well - but I cannot see how to specify a literal in > the FK constraint. What I think I need is something like: > ...CONSTRAINT notes_c1 FOREIGN KEY (foreigntablename,foreignRecNo) > REFERENCES supplier ('supplier',recno) ... When you have 2 tables, 'main' & 'dependency', with a referential integrity between them and you want to wipe all rows in dependency when the RI is deleted from main, it should be like that : main ( id int generated always as identity primary key, tm text not null ) dependency ( id int generated always as identity primary key, td text not null, ri_main int not null REFERENCES public.main ON DELETE CASCADE ) This way, when you have a row in 'dependency' that references another one from 'main', if the 'main' row is deleted, the 'dependency' row is automatically also deleted. Read the doc to see what to add if you create both rows in the same transaction. Jean-Yves