X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 504E1D1D9E6 for ; Tue, 6 Apr 2004 12:05:20 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 82603-06 for ; Tue, 6 Apr 2004 12:05:16 -0300 (ADT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 7B031D1D8B7 for ; Tue, 6 Apr 2004 12:05:16 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 38C98357C8; Tue, 6 Apr 2004 08:05:15 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 2EC7735752; Tue, 6 Apr 2004 08:05:15 -0700 (PDT) Date: Tue, 6 Apr 2004 08:05:15 -0700 (PDT) From: Stephan Szabo To: Robert Treat Cc: pgsql-sql@postgresql.org Subject: Re: partial unique constraint In-Reply-To: <1081261772.31785.48.camel@camel> Message-ID: <20040406080241.G36214@megazone.bigpanda.com> References: <1081261772.31785.48.camel@camel> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200404/40 X-Sequence-Number: 17415 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;