Re: Update more than one table

From: David Pratt <fairwinds(at)eastlink(dot)ca>
To: Roman Neuhauser <neuhauser(at)sigpipe(dot)cz>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Update more than one table
Date: 2005-07-10 18:05:30
Message-ID: 33AF286A-F16D-11D9-AD67-000A27B3B070@eastlink.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Roman. Many thanks for your reply. This is interesting and will I
give this a try and let you know how it works out. With this you are
right, application logic and transaction don't have to be separate
which would be nice for this. I was thinking the only way to solve was
a function that performed an update and returned the nextval at the
same time so that I could use that value to perform the update on next
table,etc.

Regards,
David

On Sunday, July 10, 2005, at 02:32 PM, Roman Neuhauser wrote:

> # fairwinds(at)eastlink(dot)ca / 2005-07-09 22:55:26 -0300:
>> Hi. I have a form that collects information from the user but then I
>> need to update three separate tables from what the user has submitted.
>> I could do this with application logic but I would feel it would be
>> best handled in Postgres as a transaction.
>
> Those two don't conflict.
>
>> I need to do things in this order to satisfy the foreign key
>> constraints:
>>
>> 1. Insert part of the data into 2 records of the first table (I need
>> to return theses ids so available for the next insert).
>>
>> 2. Insert part of the data into a record in a second table. The
>> id's
>> created in 1. need to be part of this record (cannot be null values)
>> and have also have referential integrity with the first table
>>
>> 3. Insert the last part of the data into a record in a third table.
>> The id created in 2 needs to be part of this record). This has
>> referential integrity with the second table.
>
> metacode:
>
> BEGIN;
> INSERT INTO first_table ...;
> SELECT currval(first_table);
> INSERT INTO first_table ...;
> SELECT currval(first_table);
> INSERT INTO second_table ...;
> INSERT INTO third_table (... currval(second_table));
> COMMIT;
>
> You can do this with any CLI, like libpq, the Perl DBI, PHP/PEAR
> pgsql_* functions or DB...
>
> --
> How many Vietnam vets does it take to screw in a light bulb?
> You don't know, man. You don't KNOW.
> Cause you weren't THERE. http://bash.org/?255991
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Matthew Terenzio 2005-07-10 21:19:21 Re: Wikipedia hackers wanted
Previous Message Roman Neuhauser 2005-07-10 17:32:11 Re: Update more than one table