Re: Alternative to Select in table check constraint

From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: Michael Glaesemann <grzm(at)seespotcode(dot)net>
Cc: General Postgresql List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Alternative to Select in table check constraint
Date: 2006-07-01 01:41:08
Message-ID: 20060701014108.79890.qmail@web31811.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2006-07-01 04:11:22 Re: Alternative to Select in table check constraint
Previous Message Michael Glaesemann 2006-07-01 00:27:07 Re: Alternative to Select in table check constraint