Re: CHECK col A not NULL if col B='x'

From: "Alexander M(dot) Pravking" <fduch(at)antar(dot)bryansk(dot)ru>
To: T E Schmitz <mailreg(at)numerixtechnology(dot)de>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: CHECK col A not NULL if col B='x'
Date: 2004-09-28 14:15:04
Message-ID: 20040928141504.GA17595@dyatel.antar.bryansk.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)~=# CREATE TABLE test (
fduch(at)~(# a integer check (case when b = 'x' then a is not null else true end),
fduch(at)~(# b text);
CREATE TABLE
fduch(at)~=# INSERT INTO test VALUES (null, '123');
INSERT 107538 1
fduch(at)~=# INSERT INTO test VALUES (null, 'x');
ERROR: new row for relation "test" violates check constraint "test_a"
fduch(at)~=# INSERT INTO test VALUES (1, 'x');
INSERT 107539 1

--
Fduch M. Pravking

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2004-09-28 14:24:16 Re: Best way to know if there is a row
Previous Message Martin Marques 2004-09-28 14:12:02 Re: CHECK col A not NULL if col B='x'