Skip site navigation (1) Skip section navigation (2)

Re: Conditional constraint?

From: Joel Burton <joel(at)joelburton(dot)com>
To: Nick Haw <nick(dot)haw(at)operagrp(dot)com>
Cc: "'pgsql-general(at)postgresql(dot)org'" <pgsql-general(at)postgresql(dot)org>,"'pgsql-novice(at)postgresql(dot)org'" <pgsql-novice(at)postgresql(dot)org>,"'pgsql-novice(at)postgresql(dot)org'" <pgsql-novice(at)postgresql(dot)org>,"'pgsql-novice(at)postgresql(dot)org'" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Conditional constraint?
Date: 2002-03-08 19:44:10
Message-ID: Pine.LNX.4.30.0203081442170.11421-100000@temp.joelburton.com (view raw or flat)
Thread:
Lists: pgsql-generalpgsql-novice
On Fri, 8 Mar 2002, Nick Haw wrote:

> Hi there, I have a problem that I can't fathom out.
>
> What I want to do is make a field so that it requires a value after a change
> in another field to a particular value. Is this possible with the ADD
> CONSTRAINT command, or will I have to make a custom function and use a
> trigger.

As in

CREATE TABLE library_patrons
  (id SERIAL PRIMARY KEY,
   age int NOT NULL,
   parents_phone VARCHAR(20) )

parents_phone could be blank, but if the patron is under 18, it should be
required?

You could make that the CONSTRAINT, however, when the age is changed, PG
won't automatically reconsider the constraint on parents_phone (unless new
data was changed in it)

So, yep, you'd have to use a trigger. Pretty straightforward to do,
though.

-- 

Joel BURTON  |  joel(at)joelburton(dot)com  |  joelburton.com  |  aim: wjoelburton
Independent Knowledge Management Consultant


In response to

pgsql-novice by date

Next:From: Patrick HatcherDate: 2002-03-08 19:58:38
Subject: Re: FATAL 1: Database template0 not accepting connection?
Previous:From: Joel BurtonDate: 2002-03-08 19:41:45
Subject: Re: FATAL 1: Database template0 not accepting connection?

pgsql-general by date

Next:From: Francisco ReyesDate: 2002-03-08 20:30:36
Subject: Re: Is vacuum full lock like old's vacuum's lock?
Previous:From: mdbDate: 2002-03-08 19:13:42
Subject: Re: Temp Tables

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group