pg.ddx.io pgsql-sql@postgresql.org mailing list archivehelp / color / mirror / Atom feed
Constraint on 2 column possible? 3+ messages / 3 participants [nested] [flat]
* Constraint on 2 column possible? @ 2005-01-27 11:44 Andrei Bintintan <klodoma@ar-sd.net> 2005-01-27 12:02 ` Re: Constraint on 2 column possible? Mike Rylander <mrylander@gmail.com> 2005-01-27 12:08 ` Re: Constraint on 2 column possible? Sean Davis <sdavis2@mail.nih.gov> 0 siblings, 2 replies; 3+ messages in thread From: Andrei Bintintan @ 2005-01-27 11:44 UTC (permalink / raw) To: pgsql-sql 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'). 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. ^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Constraint on 2 column possible? 2005-01-27 11:44 Constraint on 2 column possible? Andrei Bintintan <klodoma@ar-sd.net> @ 2005-01-27 12:02 ` Mike Rylander <mrylander@gmail.com> 1 sibling, 0 replies; 3+ messages in thread From: Mike Rylander @ 2005-01-27 12:02 UTC (permalink / raw) To: pgsql-sql On Thu, 27 Jan 2005 13:44:32 +0200, Andrei Bintintan <klodoma@ar-sd.net> 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 ^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Constraint on 2 column possible? 2005-01-27 11:44 Constraint on 2 column possible? Andrei Bintintan <klodoma@ar-sd.net> @ 2005-01-27 12:08 ` Sean Davis <sdavis2@mail.nih.gov> 1 sibling, 0 replies; 3+ messages in thread From: Sean Davis @ 2005-01-27 12:08 UTC (permalink / raw) To: Andrei Bintintan <klodoma@ar-sd.net>; +Cc: pgsql-sql On Jan 27, 2005, at 6:44 AM, 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), > ); > It looks like you want a two-column primary key for table contact and then you can reference contact(id,type). Sean > > > 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'). > > 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. ^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2005-01-27 12:08 UTC | newest] Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2005-01-27 11:44 Constraint on 2 column possible? Andrei Bintintan <klodoma@ar-sd.net> 2005-01-27 12:02 ` Mike Rylander <mrylander@gmail.com> 2005-01-27 12:08 ` Sean Davis <sdavis2@mail.nih.gov>
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox