Re: UPDATE/INSERT on multiple co-dependent tables - Mailing list pgsql-sql

From Stephan Szabo
Subject Re: UPDATE/INSERT on multiple co-dependent tables
Date
Msg-id 20041109191724.F63596@megazone.bigpanda.com
Whole thread Raw
In response to UPDATE/INSERT on multiple co-dependent tables  ("Ferindo Middleton, Jr" <fmiddleton@verizon.net>)
List pgsql-sql
On Tue, 9 Nov 2004, Ferindo Middleton, Jr wrote:

> Is it possible for an UPDATE/INSERT query string to function in such a way
> that it requires two like fields in different tables to be equal to/'in sync
> with' one another:
>
> Example: I have two tables: registration & schedules....
> they both record a class_id, start_date, end_date... I want to make sure
> that if the schedule_id field is updated in the registration table; that
> class_id, start_date & end_date fields automatically change to match the
> schedules.id record in the schedules table.... I've devised a function to
> handle this but pgsql recognizes the query to be 'infinitely recursive:
>
> CREATE RULE registration_update AS
> ON UPDATE TO registration
> DO
> UPDATE registration SET class_id = schedules.class_id WHERE new.schedule_id
> = (SELECT schedules.id FROM schedules WHERE id = new.schedule_id);
>
> What I'm doing is kind of redundant but necessary for
> backwards-compatibility

You would probably have better luck doing something like the above in a
before trigger rather than a rule by having the before trigger change
NEW.class_id to the desired value.


pgsql-sql by date:

Previous
From: "Ferindo Middleton, Jr"
Date:
Subject: UPDATE/INSERT on multiple co-dependent tables
Next
From: "Dean Gibson (DB Administrator)"
Date:
Subject: Is NULLIF nullable?