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 211759FA612 for ; Fri, 30 Jun 2006 21:27:17 -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 69438-04 for ; Fri, 30 Jun 2006 21:27:16 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from grzm.com (grzm.com [200.46.204.98]) by postgresql.org (Postfix) with ESMTP id C69EC9FA506 for ; Fri, 30 Jun 2006 21:27:16 -0300 (ADT) Received: from localhost (unknown [200.46.208.251]) by grzm.com (Postfix) with ESMTP id 9845E127E0C6 for ; Sat, 1 Jul 2006 01:27:11 +0100 (BST) Received: from grzm.com ([200.46.204.98]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 63869-05; Fri, 30 Jun 2006 21:27:11 -0300 (ADT) Received: from [172.16.1.101] (unknown [61.197.227.146]) by grzm.com (Postfix) with ESMTP id D074E127C4EB; Sat, 1 Jul 2006 01:27:05 +0100 (BST) In-Reply-To: <20060630215512.18287.qmail@web31813.mail.mud.yahoo.com> References: <20060630215512.18287.qmail@web31813.mail.mud.yahoo.com> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <4FA18A22-AFC2-4C6A-A487-57DAEF09F59F@seespotcode.net> Cc: General Postgresql List Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: Alternative to Select in table check constraint Date: Sat, 1 Jul 2006 09:27:07 +0900 To: Richard Broersma Jr X-Mailer: Apple Mail (2.752.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/337 X-Sequence-Number: 25573 On Jul 1, 2006, at 6:55 , 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) > Currently, CHECK expressions cannot contain subqueries nor refer to > variables other than columns of the current row. This is why the the above won't work. You can probably do something similar by creating UNIQUE index with a WHERE clause. For example (if I'm understanding the intent), CREATE UNIQUE INDEX one_a_badge_per_employee_idx ON badges (empno) WHERE status = 'A'; Here are links to more documentation on indexes: http://www.postgresql.org/docs/8.1/interactive/sql-createindex.html http://www.postgresql.org/docs/8.1/interactive/indexes-partial.html Hope this helps. Michael Glaesemann grzm seespotcode net