pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rod Taylor <pg@rbt.ca>
To: Richard Broersma Jr <rabroersma@yahoo.com>
Cc: Michael Glaesemann <grzm@seespotcode.net>
Cc: General Postgresql List <pgsql-sql@postgresql.org>
Subject: Re: Alternative to Select in table check constraint
Date: Sat, 01 Jul 2006 13:06:31 -0400
Message-ID: <1151773591.673.87.camel@home> (raw)
In-Reply-To: <20060701014108.79890.qmail@web31811.mail.mud.yahoo.com>
References: <20060701014108.79890.qmail@web31811.mail.mud.yahoo.com>

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.
-- 




view thread (11+ messages)  latest in thread

Message-ID: <1151773591.673.87.camel@home>
Permalink:  ../1151773591.673.87.camel@home/
Also on:    postgresql.org/message-id/1151773591.673.87.camel@home

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: pg@rbt.ca, rabroersma@yahoo.com, grzm@seespotcode.net
  Subject: Re: Alternative to Select in table check constraint
  In-Reply-To: <1151773591.673.87.camel@home>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox