Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 034D19FB8CD for ; Wed, 28 Feb 2007 07:24:07 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 55427-07 for ; Wed, 28 Feb 2007 07:23:43 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from ns1.officenet.no (ns1.officenet.no [193.212.174.3]) by postgresql.org (Postfix) with ESMTP id 5B2239FBA40 for ; Wed, 28 Feb 2007 07:23:58 -0400 (AST) Received: from [193.90.138.186] (helo=[10.14.12.130]) by ns1.officenet.no with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.60) (envelope-from ) id 1HMMuU-0007jo-Pg; Wed, 28 Feb 2007 12:23:56 +0100 From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql@postgresql.org Subject: Re: Conditional NOT NULL constraint Date: Wed, 28 Feb 2007 12:23:26 +0100 User-Agent: KMail/1.9.6 Cc: Richard Huxton References: <200702281130.47396.andreak@officenet.no> <45E5627B.5090603@archonet.com> In-Reply-To: <45E5627B.5090603@archonet.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200702281223.29076.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.594 tagged_above=0 required=5 tests=AWL, BAYES_50 X-Spam-Level: X-Archive-Number: 200702/290 X-Sequence-Number: 27624 On Wednesday 28 February 2007, Richard Huxton wrote: > 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 modifi= ed > > 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)) Certainly, thanks. =2D-=20 Andreas Joseph Krogh Senior Software Developer / Manager gpg public_key: http://dev.officenet.no/~andreak/public_key.asc =2D-----------------------+---------------------------------------------+ OfficeNet AS | The most difficult thing in the world is to | Karenslyst All=E9 11 | know how to do a thing and to watch | PO. Box 529 Sk=F8yen | somebody else doing it wrong, without | 0214 Oslo | comment. | NORWAY | | Mobile: +47 909 56 963 | | =2D-----------------------+---------------------------------------------+