X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 3C7399FA19B for ; Fri, 19 May 2006 04:01:01 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 06651-02 for ; Fri, 19 May 2006 04:00:57 -0300 (ADT) X-Greylist: delayed 17:18:57.935137 by SQLgrey- Received: from ns1.officenet.no (ns1.officenet.no [193.212.174.3]) by postgresql.org (Postfix) with ESMTP id 4D0319FA176 for ; Fri, 19 May 2006 04:00:54 -0300 (ADT) Received: from [193.212.174.32] by ns1.officenet.no with esmtps (TLSv1:EXP1024-RC4-SHA:128) (Exim 4.60) (envelope-from ) id 1Fgyyg-0002oF-CN for pgsql-sql@postgresql.org; Fri, 19 May 2006 09:00:53 +0200 From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql@postgresql.org Subject: Re: Constraint question Date: Thu, 18 May 2006 21:05:55 +0200 User-Agent: KMail/1.9.1 References: <200605181541.54299.andreak@officenet.no> <446CA301.20305@logix-tt.com> In-Reply-To: <446CA301.20305@logix-tt.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200605182105.55753.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/151 X-Sequence-Number: 25182 On Thursday 18 May 2006 18:38, Markus Schaber wrote: > Hi, Andreas, > > Andreas Joseph Krogh wrote: > > create table onp_crm_businessfield_company( > > businessfield_id integer not null references onp_crm_businessfield(id), > > company_id integer not null references onp_crm_relation(id), > > is_preferred boolean, > > UNIQUE(businessfield_id, company_id) > > ); > > > > I want a constraint on "is_preffered" so that it's only allowed to be s= et > > once pr. businessfield_id pr. company so that only one businessfield can > > be preferred for a company. Does anyone have an idea how to enforce thi= s? > > CREATE UNIQUE INDEX foo ON onp_crm_businessfield_company(company_id) > WHERE is_prefferred; Thanks. I also figured out that an index like this also works: UNIQUE(company_id, is_preferred) This works because NULL =3D NULL is false. But I guess your solution is cle= aner. =2D-=20 Andreas Joseph Krogh Senior Software Developer / Manager gpg public_key: http://dev.officenet.no/~andreak/public_key.asc =2D-----------------------+---------------------------------------------+ OfficeNet AS | The most difficult thing in the world is to | Hoffsveien 17 | know how to do a thing and to watch | PO. Box 425 Sk=F8yen | somebody else doing it wrong, without | 0213 Oslo | comment. | NORWAY | | Phone : +47 22 13 01 00 | | Direct: +47 22 13 10 03 | | Mobile: +47 909 56 963 | | =2D-----------------------+---------------------------------------------+