Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 4903B475E68 for ; Tue, 21 May 2002 10:44:05 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id AD12247617A for ; Tue, 21 May 2002 10:43:58 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id E0B25D602; Tue, 21 May 2002 07:43:59 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id D45355C03; Tue, 21 May 2002 07:43:59 -0700 (PDT) Date: Tue, 21 May 2002 07:43:59 -0700 (PDT) From: Stephan Szabo To: Markus Wagner Cc: Subject: Re: constraints for ensuring relationships In-Reply-To: <0205211208080R.01320@magnus> Message-ID: <20020521073832.K11485-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200205/220 X-Sequence-Number: 7855 On Tue, 21 May 2002, Markus Wagner wrote: > I wonder if I can use constraints to ensure some special integrity. > > Suppose there is a table "Type" of item types: > > CREATE TABLE "Type" > ( > "index" SERIAL PRIMARY KEY, > "name" TEXT, > "multiple" BOOL > ); > > Suppose there is a table "Item" like this: > > CREATE TABLE "Item" > ( > "index" SERIAL PRIMARY KEY, > "type" INT REFERENCES "Type", > "data" TEXT > ); > > Supose there is a table "SubItem" that contains special details about our > items: > > CREATE TABLE "SubItem" > ( > "index" SERIAL PRIMARY KEY, > "Item" INT REFERENCES "Item", > "Data" TEXT > ); > > And now the constraint: > > Each Item should be allowed to have more than one subitem, if and only if its > type allows for multiple instances, i. e. the type the item is linked to must > have 't' in its field 'multiple'. > > Can I attach constraints to some of the tables (e. g. "SubItem") that ensure > this? Given your schema, you might be able to get away with triggers that do the check or a bunch of check constraints that call a function that does the check. You need to worry about updates on Type and Item and inserts and updates on SubItem I think.