pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Andreas Joseph Krogh <andreak@officenet.no>
Cc: pgsql-sql@postgresql.org
Subject: Re: Conditional NOT NULL constraint
Date: Wed, 28 Feb 2007 11:07:39 +0000
Message-ID: <45E5627B.5090603@archonet.com> (raw)
In-Reply-To: <200702281130.47396.andreak@officenet.no>
References: <200702281130.47396.andreak@officenet.no>

Andreas Joseph Krogh wrote:
> Hi all!
> Is there a simple way to add a "NOT NULL constraint" to a column without using 
> a trigger if another column is not null?
> Something like this:
> 
> CREATE TABLE activity(
> id SERIAL primary key,
> name varchar not null,
> created timestamp not null default now(),
> modified timestamp,
> created_by integer not null,
> modified_by integer
> );
> 
> alter table activity alter column modified_by set not null where modified is 
> not null;
> 
> I want a constraint which says: "modified_by not null if modified is not 
> null".

Would a CHECK do? Something like:

ALTER TABLE activity ADD CONSTRAINT both_modified_set CHECK 
((modified_by IS NULL AND modified IS NULL) OR (modified_by IS NOT NULL 
AND modified IS NOT NULL))


-- 
   Richard Huxton
   Archonet Ltd



view thread (3+ messages)  latest in thread

Message-ID: <45E5627B.5090603@archonet.com>
Permalink:  ../45E5627B.5090603@archonet.com/
Also on:    postgresql.org/message-id/45E5627B.5090603@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, andreak@officenet.no
  Subject: Re: Conditional NOT NULL constraint
  In-Reply-To: <45E5627B.5090603@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox