Re: Check constraint - Mailing list pgsql-general

From Gregory Wood
Subject Re: Check constraint
Date
Msg-id 40588B2C.4020504@ewebengine.com
Whole thread Raw
In response to Re: Check constraint  (Richard Huxton <dev@archonet.com>)
Responses Re: Check constraint
List pgsql-general
>>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)
>
> No - you'll want a BEFORE UPDATE/INSERT trigger. It shouldn't be difficult to
> do though.

I don't see why not:

CHECK ((purchase_type <> 3) OR ((purchase_type = 3) AND comment IS NOT
NULL))


pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: Smallint - Integer Casting Problems in Plpgsql functions
Next
From: Bruno Wolff III
Date:
Subject: Re: Check constraint