Re: partial unique constraint

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Robert Treat <xzilla(at)users(dot)sourceforge(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: partial unique constraint
Date: 2004-04-06 15:05:15
Message-ID: 20040406080241.G36214@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Tue, 6 Apr 2004, Robert Treat wrote:

> Trying to come up with the proper syntax to meet the following criteria:
>
> create table foo (bar integer, baz boolean UNIQUE (bar, baz = true));
>
> note the above syntax is not correct, but should demonstrate what i'm
> trying to do; I want to add a unique constraint such that we only allow
> one case of bar and baz = true... i can have unlimited bar and baz =
> false, and there can be multiple bar and baz = true if the bars are
> different... did some doc reading and mail list searching but a valid
> syntax for this seems to be escaping me...
>
> btw I'm pretty sure I could do this with an external trigger, but am
> wondering about a constraint oriented approach

You can't precisely with a constraint (AFAIR sql is marginally limited in
that way at least in 92 and 99), but you should be able to do it with a
partial unique index something like
create unique index foo_i on foo(bar) where baz is true;

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message ries 2004-04-06 15:05:37 Re: partial unique constraint
Previous Message Tom Lane 2004-04-06 15:00:52 Re: could not devise a query plan