Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 7CAE99FB694 for ; Wed, 28 Feb 2007 07:07:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 92778-05 for ; Wed, 28 Feb 2007 07:07:40 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id D3AE59FB64D for ; Wed, 28 Feb 2007 07:07:40 -0400 (AST) Received: from server3.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id C26CE76D10; Wed, 28 Feb 2007 11:07:39 +0000 (GMT) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 8E6E9274092; Wed, 28 Feb 2007 11:07:39 +0000 (GMT) Message-ID: <45E5627B.5090603@archonet.com> Date: Wed, 28 Feb 2007 11:07:39 +0000 From: Richard Huxton User-Agent: IceDove 1.5.0.9 (X11/20061220) MIME-Version: 1.0 To: Andreas Joseph Krogh Cc: pgsql-sql@postgresql.org Subject: Re: Conditional NOT NULL constraint References: <200702281130.47396.andreak@officenet.no> In-Reply-To: <200702281130.47396.andreak@officenet.no> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.212 tagged_above=0 required=5 tests=AWL, BAYES_50, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200702/289 X-Sequence-Number: 27623 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