Re: Check constraint

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Francisco Reyes <lists(at)natserv(dot)com>
Cc: pgsql General List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Check constraint
Date: 2004-03-17 17:49:34
Message-ID: 20040317174934.GA15400@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Mar 17, 2004 at 12:03:04 +0000,
Francisco Reyes <lists(at)natserv(dot)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)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Valter 2004-03-17 17:52:34 Relation "xxxxx" does not exist
Previous Message Gregory Wood 2004-03-17 17:30:20 Re: Check constraint