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 AAADF9FA7B6 for ; Fri, 30 Jun 2006 22:41:13 -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 56679-04 for ; Fri, 30 Jun 2006 22:41:09 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web31811.mail.mud.yahoo.com (web31811.mail.mud.yahoo.com [68.142.207.74]) by postgresql.org (Postfix) with SMTP id 7C7B99FA612 for ; Fri, 30 Jun 2006 22:41:09 -0300 (ADT) Received: (qmail 79892 invoked by uid 60001); 1 Jul 2006 01:41:08 -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=XHyMNSmVMT4Kz/vIfYqBlXiUtY1T97c0HPyuCI84AENATk4LoIAG9GT76IeQ+0MAgZ8RqwQESU7T0ETHx3uZEzv/Iaa5Vgx4o/wuVbQCEGSsSE3SK7Gm1IhZpi+6+z4WA8kXlb8jKJfbiiS5x7r/hKu0HZjYpDcnBSL3Y2oSIgw= ; Message-ID: <20060701014108.79890.qmail@web31811.mail.mud.yahoo.com> Received: from [24.54.196.198] by web31811.mail.mud.yahoo.com via HTTP; Fri, 30 Jun 2006 18:41:08 PDT Date: Fri, 30 Jun 2006 18:41:08 -0700 (PDT) From: Richard Broersma Jr Subject: Re: Alternative to Select in table check constraint To: Michael Glaesemann Cc: General Postgresql List In-Reply-To: <4FA18A22-AFC2-4C6A-A487-57DAEF09F59F@seespotcode.net> 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: 200606/338 X-Sequence-Number: 25574 > > 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? Oh well, partial index are obvious the superior solution since the entire table doesn't not have to be scanned to determine if the new badge can be set to active. Once again thanks for the insight. Regards, Richard Broersma Jr.