Re: CHECK constraint so that only one is TRUE?

From: jboes(at)nexcerpt(dot)com (Jeff Boes)
To: pgsql-general(at)postgresql(dot)org
Subject: Re: CHECK constraint so that only one is TRUE?
Date: 2002-01-15 16:34:24
Message-ID: d40a65a1.0201150834.930d577@posting.google.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

incognit(at)unifiedmind(dot)com (+I) wrote in message news:<ebcd475a(dot)0201121104(dot)62d46853(at)posting(dot)google(dot)com>...
> How would you write a check constraint to ensure that only one column,
> of type boolean, is set to TRUE for a given ID?

Need more info. Are you saying that a row has two or more boolean
columns, and for each row you want to ensure that only one is TRUE?

E.g.,

CREATE TABLE foo (
id integer not null,
is_animal boolean not null,
is_vegetable boolean not null,
is_mineral boolean not null,
check(?));

If this is the case, and you need a generic solution, you'd best write
a function to compute it. There's a boolean expression for this,

((a xor b xor c) or (a xor b) and (b xor c) and (a xor c))

(and there's probably a shorter one) but it's hard to generalize it to
N columns.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2002-01-15 16:47:38 Re: PostgreSQL 7.1.3 recovery tool "pg_check" no longer works
Previous Message Colm McCartan 2002-01-15 16:09:10 Re: [GENERAL] OT: anon CVS hassles