Re: Check constraint - Mailing list pgsql-general

From Bruno Wolff III
Subject Re: Check constraint
Date
Msg-id 20040317174934.GA15400@wolff.to
Whole thread Raw
In response to Check constraint  (Francisco Reyes <lists@natserv.com>)
List pgsql-general
On Wed, Mar 17, 2004 at 12:03:04 +0000,
  Francisco Reyes <lists@natserv.com> wrote:
> I have a "comment" field in a table that I want populated if another field
> has a certain value. Is it possible to set a check constraint for this?
>
> Example:
> Let's say we have fields
> Purchase_type smallint check(purchase_type <4)
> comment       varchar
>
> I need a check rule to something like (pseudo code)
> check (if purchase_type = 3 then NOT NULL comment)

As long as the fields are in the same table you can do this. But you have
to use the IS NOT NULL function rather than try to activate a NOT NULL
constraint. The check would look something like:
check (purchase_type <> 3 OR IS NOT NULL comment)

pgsql-general by date:

Previous
From: Gregory Wood
Date:
Subject: Re: Check constraint
Next
From: "Valter"
Date:
Subject: Relation "xxxxx" does not exist