From klodoma@ar-sd.net Thu Jan 27 11:44:40 2005 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 C451F3A5E1B for ; Thu, 27 Jan 2005 11:44:40 +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 46910-03 for ; Thu, 27 Jan 2005 11:44:28 +0000 (GMT) Received: from ar-sd.net (unknown [82.77.155.72]) by svr1.postgresql.org (Postfix) with ESMTP id 2E3003A5E7C for ; Thu, 27 Jan 2005 11:44:30 +0000 (GMT) Received: from localhost (localhost [127.0.0.1]) by ar-sd.net (Postfix) with ESMTP id AD8E51EBF1 for ; Thu, 27 Jan 2005 13:39:28 +0200 (EET) Received: from ar-sd.net ([127.0.0.1]) by localhost (linz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 13422-02 for ; Thu, 27 Jan 2005 13:39:27 +0200 (EET) Received: from forge (unknown [192.168.0.11]) by ar-sd.net (Postfix) with SMTP id B81701EBD7 for ; Thu, 27 Jan 2005 13:39:26 +0200 (EET) Message-ID: <013b01c50465$93854580$0b00a8c0@forge> From: "Andrei Bintintan" To: Subject: Constraint on 2 column possible? Date: Thu, 27 Jan 2005 13:44:32 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0138_01C50476.54D1E120" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Virus-Scanned: by amavisd-new at ar-sd.net X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.013 tagged_above=0 required=5 tests=AWL, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200501/281 X-Sequence-Number: 20069 This is a multi-part message in MIME format. ------=_NextPart_000_0138_01C50476.54D1E120 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi,=20 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.=20 How can I write a constraint that checks that id_hr references = contact(id) and the contact(type=3D't1') and that id_wk references contact(id) and the contact(type=3D't2').=20 More explicit: the id_hr shows to the id from contact, and this line = from contact must have the line type=3D'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. ------=_NextPart_000_0138_01C50476.54D1E120 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
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=20 varchar(20),
);
 
 
Now id_hr and id_wk are all referencing = the same=20 table contact(id). In the contact table I have another column called = type.=20
How can I write a constraint that = checks that id_hr=20 references contact(id) and the contact(type=3D't1')
and that id_wk references contact(id) = and the=20 contact(type=3D't2').
 
More explicit: the id_hr shows to the = id from=20 contact, and this line from contact must have the line type=3D't1'. The = same for=20 id_wk just the type is another.
 
I can write:
CREATE = TABLE werke1(
id=20 SERIAL,
id_hr int4=20 NOT NULL=20 references contact(id),
id_wk int4 NOT NULL references contact(id)
);
but how do I check also the type=20 column?
 
Best regards,
Andy.
------=_NextPart_000_0138_01C50476.54D1E120-- From mrylander@gmail.com Thu Jan 27 12:02:48 2005 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 From sdavis2@mail.nih.gov Thu Jan 27 12:08:09 2005 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 A0BA03A5E0A for ; Thu, 27 Jan 2005 12:08:09 +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 50071-03 for ; Thu, 27 Jan 2005 12:08:05 +0000 (GMT) Received: from mx5.net.nih.gov (mx5.net.nih.gov [165.112.130.36]) by svr1.postgresql.org (Postfix) with ESMTP id 0E1413A5E8C for ; Thu, 27 Jan 2005 12:08:05 +0000 (GMT) Received: from mx5.net.nih.gov (localhost [127.0.0.1]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j0RC84iS002108 for ; Thu, 27 Jan 2005 07:08:04 -0500 Received: from [128.231.145.14] (holmes.nhgri.nih.gov [128.231.145.14]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j0RC84QO002089; Thu, 27 Jan 2005 07:08:04 -0500 In-Reply-To: <013b01c50465$93854580$0b00a8c0@forge> References: <013b01c50465$93854580$0b00a8c0@forge> Mime-Version: 1.0 (Apple Message framework v619) Content-Type: text/plain; charset=ISO-8859-1; format=flowed Message-Id: <18D0F33A-705C-11D9-8FBF-000D933565E8@mail.nih.gov> Content-Transfer-Encoding: quoted-printable Cc: From: Sean Davis Subject: Re: Constraint on 2 column possible? Date: Thu, 27 Jan 2005 07:08:03 -0500 To: "Andrei Bintintan" X-Mailer: Apple Mail (2.619) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.013 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200501/283 X-Sequence-Number: 20071 On Jan 27, 2005, at 6:44 AM, Andrei Bintintan wrote: > Hi, > =A0 > I have a table: > CREATE TABLE werke1( > id SERIAL, > id_hr int4 NOT NULL, > id_wk int4 NOT NULL > ); > =A0 > CREATE TABLE contact( > id SERIAL, > type varchar(20), > ); > It looks like you want a two-column primary key for table contact and=20 then you can reference contact(id,type). Sean > =A0 > =A0 > Now id_hr and id_wk are all referencing the same table contact(id). In=20= > the contact table I have another column called type. > How can I write a constraint that checks that id_hr references=20 > contact(id) and the contact(type=3D't1') > and that id_wk references contact(id) and the contact(type=3D't2'). > =A0 > More explicit: the id_hr shows to the id from contact, and this line=20= > from contact must have the line type=3D't1'. The same for id_wk just = the=20 > type is another. > =A0 > 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? > =A0 > Best regards, > Andy.