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 542909FA667 for ; Sun, 2 Jul 2006 22:50:35 -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 05248-02 for ; Sun, 2 Jul 2006 22:50:27 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web31815.mail.mud.yahoo.com (web31815.mail.mud.yahoo.com [68.142.206.168]) by postgresql.org (Postfix) with SMTP id 3848B9FA60C for ; Sun, 2 Jul 2006 22:50:26 -0300 (ADT) Received: (qmail 2193 invoked by uid 60001); 3 Jul 2006 01:50:25 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=t4AlVGAz2YcbLaGU3JzFaRxNnWBKPeWxfhOCR9qwgz9e1ntVJNBuUHuW/CHlf5Vh727/kLFAKpX/XqtUZ5jnqT7u8aO7J6sBAfna45hnx5zY/hQev0C56E+f4Qf8kCBV6C0UQ8V1mcLBJ4Q7dTFVn/V/GpP396wMMIh8xTNI7lc= ; Message-ID: <20060703015025.2191.qmail@web31815.mail.mud.yahoo.com> Received: from [24.54.196.198] by web31815.mail.mud.yahoo.com via HTTP; Sun, 02 Jul 2006 18:50:25 PDT Date: Sun, 2 Jul 2006 18:50:25 -0700 (PDT) From: Richard Broersma Jr Subject: Re: Alternative to Select in table check constraint To: Rod Taylor Cc: Michael Glaesemann , General Postgresql List In-Reply-To: <1151773591.673.87.camel@home> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/13 X-Sequence-Number: 25587 > 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. Thanks for the Input Rod. I will try implementing a trigger as a way to constrain the input data to see how it works. Regards, Richard Broersma Jr.