Re: CHECK col A not NULL if col B='x' - Mailing list pgsql-sql

From Alexander M. Pravking
Subject Re: CHECK col A not NULL if col B='x'
Date
Msg-id 20040928141504.GA17595@dyatel.antar.bryansk.ru
Whole thread Raw
In response to CHECK col A not NULL if col B='x'  (T E Schmitz <mailreg@numerixtechnology.de>)
List pgsql-sql
On Tue, Sep 28, 2004 at 03:02:02PM +0100, T E Schmitz wrote:
> Hello,
> 
> Is it possible to set up a table CHECK, which ensures that column A is 
> NOT NULL if column B = 'x' ?

Sure.

fduch@~=# CREATE TABLE test (
fduch@~(# a integer check (case when b = 'x' then a is not null else true end),
fduch@~(# b text);
CREATE TABLE
fduch@~=# INSERT INTO test VALUES (null, '123');
INSERT 107538 1
fduch@~=# INSERT INTO test VALUES (null, 'x');
ERROR:  new row for relation "test" violates check constraint "test_a"
fduch@~=# INSERT INTO test VALUES (1, 'x');
INSERT 107539 1

-- 
Fduch M. Pravking


pgsql-sql by date:

Previous
From: Martin Marques
Date:
Subject: Re: CHECK col A not NULL if col B='x'
Next
From: Achilleus Mantzios
Date:
Subject: Re: FOREIGN KEY and AccessExclusiveLock