Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UXBi3-0004cx-N5 for pgsql-sql@arkaria.postgresql.org; Tue, 30 Apr 2013 14:39:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UXBi2-0002Iu-UH for pgsql-sql@arkaria.postgresql.org; Tue, 30 Apr 2013 14:39:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UXBi2-0002Ip-83 for pgsql-sql@postgresql.org; Tue, 30 Apr 2013 14:39:10 +0000 Received: from mout.gmx.net ([212.227.17.22]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UXBhz-0000hb-6z for pgsql-sql@postgresql.org; Tue, 30 Apr 2013 14:39:09 +0000 Received: from mailout-de.gmx.net ([10.1.76.35]) by mrigmx.server.lan (mrigmx001) with ESMTP (Nemesis) id 0MFfRr-1UIcrX2Grl-00Eedd for ; Tue, 30 Apr 2013 16:39:06 +0200 Received: (qmail invoked by alias); 30 Apr 2013 14:39:06 -0000 Received: from p5082D1AD.dip0.t-ipconnect.de (EHLO GK1C06) [80.130.209.173] by mail.gmx.net (mp035) with SMTP; 30 Apr 2013 16:39:06 +0200 X-Authenticated: #41312857 X-Provags-ID: V01U2FsdGVkX18SI3ldceQSZ8NkuiF0H3NRhx1rvBdKu8jFV6NmBT Bd+TdkqpUY+dnw Date: Tue, 30 Apr 2013 16:39:05 +0200 From: Wolfgang Keller To: pgsql-sql@postgresql.org Subject: Correct implementation of 1:n relationship with n>0? Message-Id: <20130430163905.77d43412c0176c3d9ebd8d90@gmx.net> X-Mailer: Sylpheed 3.3.0 (GTK+ 2.10.14; i686-pc-mingw32) Mail-Copies-To: never Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Pg-Spam-Score: -4.4 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org It hit me today that a 1:n relationship can't be implemented just by a single foreign key constraint if n>0. I must have been sleeping very deeply not to notice this. E.g. if there is a table "list" and another table "list_item" and the relationship can be described as "every list has at least one list_item" (and every list_item can only be part of one list, but this is trivial). A "correct" solution would require (at least?): 1. A foreign key pointing from each list_item to its list 2. Another foreign key pointing from each list to one of its list_item. But this must be a list_item that itself points to the same list, so just a simple foreign key constraint doesn't do it. 3. When a list has more than one list_item, and you want to delete the list_item that its list points to, you have to "re-point" the foreign key constraint on the list first. Do I need to use stored proceures then for all insert, update, delete actions? (4. Anything else that I've not seen?) Is there a "straight" (and tested) solution for this in PostgreSQL, that someone has already implemented and that can be re-used? No, I definitely don't want to get into programming PL/PgSQL myself. especially if the solution has to warrant data integrity under all circumstances. Such as concurrent update, insert, delete etc. TIA, Sincerely, Wolfgang -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql