X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id B68069DC868 for ; Sun, 5 Mar 2006 07:50:37 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 80744-08 for ; Sun, 5 Mar 2006 07:50:39 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtp-send.myrealbox.com (smtp-send.myrealbox.com [151.155.5.143]) by postgresql.org (Postfix) with ESMTP id 490109DCB9F for ; Sun, 5 Mar 2006 07:50:35 -0400 (AST) Received: from [172.16.1.180] grzm [61.197.227.146] by smtp-send.myrealbox.com with NetMail SMTP Agent $Revision: 1.6 $ on Linux; Sun, 05 Mar 2006 04:50:31 -0700 In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v746.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: Check/unique constraint question Date: Sun, 5 Mar 2006 20:50:28 +0900 To: Jeff Frost X-Mailer: Apple Mail (2.746.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.451 required=5 tests=[AWL=0.451] X-Spam-Score: 0.451 X-Spam-Level: X-Archive-Number: 200603/92 X-Sequence-Number: 24438 On Mar 5, 2006, at 17:25 , Jeff Frost wrote: > And would like to make a unique constraint which would only check > the uniqueness of id if active=true. I believe you're looking for what is called a partial index. http://www.postgresql.org/docs/current/interactive/indexes-partial.html Note, I've added a foo_id column to make sure each row is unique. (Duplicates are a Bad Thing.) create table foo ( foo_id serial not null , id integer not null , active boolean not null ); create unique index foo_partial_idx on foo (id) where active; insert into foo (id, active) values (5, false); insert into foo (id, active) values (5, false); insert into foo (id, active) values (5, true); insert into foo (id, active) values (6, false); insert into foo (id, active) values (6, true); select * from foo; foo_id | id | active --------+----+-------- 1 | 5 | f 2 | 5 | f 3 | 5 | t 4 | 6 | f 5 | 6 | t (5 rows) insert into foo (id, active) values (5, true); ERROR: duplicate key violates unique constraint "foo_partial_idx" Michael Glaesemann grzm myrealbox com