Re: CHECK constraint so that only one is TRUE? - Mailing list pgsql-general

From jboes@nexcerpt.com (Jeff Boes)
Subject Re: CHECK constraint so that only one is TRUE?
Date
Msg-id d40a65a1.0201150834.930d577@posting.google.com
Whole thread Raw
List pgsql-general
incognit@unifiedmind.com (+I) wrote in message news:<ebcd475a.0201121104.62d46853@posting.google.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.

pgsql-general by date:

Previous
From: "Artigas, Ricardo Y."
Date:
Subject: Re: Is there is shutdown utility for postgresql?
Next
From: "Harald Massa"
Date:
Subject: Different views with same name for different users