X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B34FF3A5E0A for ; Thu, 27 Jan 2005 12:02:48 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 49530-01 for ; Thu, 27 Jan 2005 12:02:41 +0000 (GMT) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.202]) by svr1.postgresql.org (Postfix) with ESMTP id E9C783A5C5A for ; Thu, 27 Jan 2005 12:02:43 +0000 (GMT) Received: by rproxy.gmail.com with SMTP id c51so235505rne for ; Thu, 27 Jan 2005 04:02:43 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=sIWfKyOGovsk+Cv/2z6DKZYmIuHIjjCyR48aFBFzq40pvr9Itxkwj3VX64uvZrsA+MIBDlF3paccMdOtEQpF0jzI7yY45pJSGyFClNVHy72hyYRGK/Vs4ToS4+s20+AKU84vGRr763VE0ymnIMz8ybMRCIxLo7JTRnQqYw2e/Og= Received: by 10.38.208.23 with SMTP id f23mr270574rng; Thu, 27 Jan 2005 04:02:43 -0800 (PST) Received: by 10.38.126.18 with HTTP; Thu, 27 Jan 2005 04:02:43 -0800 (PST) Message-ID: Date: Thu, 27 Jan 2005 12:02:43 +0000 From: Mike Rylander Reply-To: Mike Rylander To: PostgreSQL SQL Subject: Re: Constraint on 2 column possible? In-Reply-To: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <013b01c50465$93854580$0b00a8c0@forge> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.085 tagged_above=0 required=5 tests=AWL, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200501/282 X-Sequence-Number: 20070 On Thu, 27 Jan 2005 13:44:32 +0200, Andrei Bintintan wrote: > > Hi, > > I have a table: > CREATE TABLE werke1( > id SERIAL, > id_hr int4 NOT NULL, > id_wk int4 NOT NULL > ); > > CREATE TABLE contact( > id SERIAL, > type varchar(20), > ); > > > Now id_hr and id_wk are all referencing the same table contact(id). In the > contact table I have another column called type. > How can I write a constraint that checks that id_hr references contact(id) > and the contact(type='t1') > and that id_wk references contact(id) and the contact(type='t2'). If I understand what you want, you can do this with a multi-column foreign key and check constraints. CREATE TABLE werke1 ( id SERIAL, id_hr NOT NULL, hr_contact NOT NULL CHECK (hr_contact = 't1'), id_wk int4 NOT NULL, wk_contact NOT NULL CHECK (hr_contact = 't2'), CONSTRAINT werke1_hr FOREIGN KEY (id_hr,hr_contact) references contact(id,type), CONSTRAINT werke1_wk FOREIGN KEY (id_wk, wk_contact) references contact(id,type) ); This will cause the FKEY to match only contact entries that have the correct combination of id and type. Hope that helps! > > More explicit: the id_hr shows to the id from contact, and this line from > contact must have the line type='t1'. The same for id_wk just the type is > another. > > I can write: > CREATE TABLE werke1( > id SERIAL, > id_hr int4 NOT NULL references contact(id), > id_wk int4 NOT NULL references contact(id) > ); > but how do I check also the type column? > > Best regards, > Andy. -- Mike Rylander mrylander@gmail.com GPLS -- PINES Development Database Developer http://open-ils.org -- Mike Rylander mrylander@gmail.com GPLS -- PINES Development Database Developer http://open-ils.org