Обсуждение: Conditional constraint?

Поиск
Список
Период
Сортировка

Conditional constraint?

От
Nick Haw
Дата:
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.
 
Many thanks
 
Nick 
 

Re: Conditional constraint?

От
Joel Burton
Дата:
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@joelburton.com  |  joelburton.com  |  aim: wjoelburton
Independent Knowledge Management Consultant


Re: [NOVICE] Conditional constraint?

От
"Josh Berkus"
Дата:
Nick,

> 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.

That depends on how complicated your rules are.  All constraints must
 evaluate to a true/false statement, so complex procedures require a
 function + trigger instead.  How about posting some more details?

-Josh