Updating in multiple tables

Started by Luuk Jansenabout 20 years ago3 messagesgeneral
Jump to latest
#1Luuk Jansen
subscribe@angelosystems.com

I have a quick newbee question:

Is is possible to update in multiple tables at the same time.
I am working with PHP scripts and will use the following example to
explain what I want to do:

I have a generic_user tables, which forms a INNER JOIN with the
logon_user table (with a join using id).

generic_user:
- id
- name
- passowrd

logon_user:
- id
- last_logon
- nickname

As I load all the fields at once in an array, and want to update the
same, so just something like

"UPDATE generic_user INNER JOIN logon_user USING (id) SET name='test',
nickname='test2' WHERE id = 1";

Is there anybody who can explain how to do this?

Thanks in advance,
Regards,
Luuk

#2Tino Wildenhain
tino@wildenhain.de
In reply to: Luuk Jansen (#1)
Re: Updating in multiple tables

Luuk Jansen schrieb:

I have a quick newbee question:

Is is possible to update in multiple tables at the same time.
I am working with PHP scripts and will use the following example to
explain what I want to do:

I have a generic_user tables, which forms a INNER JOIN with the
logon_user table (with a join using id).

generic_user:
- id
- name
- passowrd

logon_user:
- id
- last_logon
- nickname

As I load all the fields at once in an array, and want to update the
same, so just something like

"UPDATE generic_user INNER JOIN logon_user USING (id) SET name='test',
nickname='test2' WHERE id = 1";

Is there anybody who can explain how to do this?

You either update both tables in subsequent update statements
(in one exec call or inside your transaction)
or create an updateable view.
(Just like aregular view and then add a rule for update - maybe
using a call to a stored function)
or just use the stored function directly.

HTH
Tino

#3Luuk Jansen
subscribe@angelosystems.com
In reply to: Tino Wildenhain (#2)
Re: Updating in multiple tables

Thanks for the quick reply,

Using a view and rules seems to be the way to do it as it saves me from
rewriting all classes in my application that save data.

Is there any simple way to create a rule that on the update of the view
'detects' which field belongs to which table, and updates them
accordingly?
The application now just parses the fields of the joined tables to the
query without any info on which table it came from (as it is the same
array returned from the select function, which only gives the field
names).

I assume one might have to use functions for it, but I never used them.

Luuk

Show quoted text

On Mon, 2006-03-20 at 15:34 +0100, Tino Wildenhain wrote:

Luuk Jansen schrieb:

I have a quick newbee question:

Is is possible to update in multiple tables at the same time.
I am working with PHP scripts and will use the following example to
explain what I want to do:

I have a generic_user tables, which forms a INNER JOIN with the
logon_user table (with a join using id).

generic_user:
- id
- name
- passowrd

logon_user:
- id
- last_logon
- nickname

As I load all the fields at once in an array, and want to update the
same, so just something like

"UPDATE generic_user INNER JOIN logon_user USING (id) SET name='test',
nickname='test2' WHERE id = 1";

Is there anybody who can explain how to do this?

You either update both tables in subsequent update statements
(in one exec call or inside your transaction)
or create an updateable view.
(Just like aregular view and then add a rule for update - maybe
using a call to a stored function)
or just use the stored function directly.

HTH
Tino

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend