X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A22009FA5CD for ; Sat, 1 Jul 2006 14:09:14 -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 44619-02 for ; Sat, 1 Jul 2006 14:09:08 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from gozer.look.ca (epsilon1.look.ca [207.136.80.10]) by postgresql.org (Postfix) with ESMTP id 956589FA5C3 for ; Sat, 1 Jul 2006 14:09:08 -0300 (ADT) Received: from 209-161-223-37.dsl.look.ca ([209.161.223.37]) by gozer.look.ca with esmtp (Exim 4.20) id 1Fwixu-0002u7-2A; Sat, 01 Jul 2006 17:09:06 +0000 From: Rod Taylor To: Richard Broersma Jr Cc: Michael Glaesemann , General Postgresql List In-Reply-To: <20060701014108.79890.qmail@web31811.mail.mud.yahoo.com> References: <20060701014108.79890.qmail@web31811.mail.mud.yahoo.com> Date: Sat, 01 Jul 2006 13:06:31 -0400 Message-Id: <1151773591.673.87.camel@home> Mime-Version: 1.0 X-Mailer: Evolution 2.6.2 FreeBSD GNOME Team Port X-SA-Exim-Mail-From: pg@rbt.ca Subject: Re: Alternative to Select in table check constraint Content-Type: text/plain Content-Transfer-Encoding: 7bit X-SA-Exim-Version: 3.1 (built Tue Feb 24 05:09:27 GMT 2004) X-SA-Exim-Scanned: Yes X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/8 X-Sequence-Number: 25582 On Fri, 2006-06-30 at 18:41 -0700, Richard Broersma Jr wrote: > > > CHECK ( 1 = ALL ( SELECT COUNT(STATUS) > > > FROM BADGES > > > WHERE STATUS = 'A' > > > GROUP BY EMPNO)) > > > > From the PostgreSQL docs (http://www.postgresql.org/docs/8.1/ > > interactive/sql-createtable.html) > > > > CREATE UNIQUE INDEX one_a_badge_per_employee_idx > > ON badges (empno) > > WHERE status = 'A'; > > http://www.postgresql.org/docs/8.1/interactive/sql-createindex.html > > http://www.postgresql.org/docs/8.1/interactive/indexes-partial.html > > Michael, > > Partial indexs seem to be "what the doctor ordered!" And your suggest is right on, the idea of > the constraint is to allow only one active badge status at a time. > > But now that I think about it, using the authors suggestion (if it actually worked), how would > would it be possible to change the active status from one badge to another? Unset the status first then set on the new one. Same transaction of course. You may find this type of constraint is more workable with a TRIGGER deferred until commit time than a unique constraint which cannot (at this time in PostgreSQL) be deferred. --