Thread: Update in all tables

Update in all tables

From
Judith
Date:
   Hello everybody I need to update a field with the same value in the 
tables of my data base but this field exists in almost all tables and 
has the same value, I don't want to code a script, so my question is if 
there is some way to update that field with a query and affects all the 
tables that contain the field?
   Thanks in advanced, regards!!!



Re: Update in all tables

From
Janning Vygen
Date:
Am Mittwoch, 22. Februar 2006 19:08 schrieb Judith:
>    Hello everybody I need to update a field with the same value in the
> tables of my data base but this field exists in almost all tables and
> has the same value, I don't want to code a script, so my question is if
> there is some way to update that field with a query and affects all the
> tables that contain the field?

no. but you can do with a trigger ON UPDATE

but what is so evil about a script like this:

#!/bin/sh
TABLES="A B C D"
for TAB in $TABLES
do psql -c "UPDATE $TAB set field = 'new' where field = 'old'" mydbname;
done



Re: Update in all tables

From
Scott Marlowe
Date:
On Wed, 2006-02-22 at 12:08, Judith wrote:
>    Hello everybody I need to update a field with the same value in the 
> tables of my data base but this field exists in almost all tables and 
> has the same value, I don't want to code a script, so my question is if 
> there is some way to update that field with a query and affects all the 
> tables that contain the field?

Are those fields all dependent FK fields?  If so, then declaring those
foreign keys as on update cascade is all you need.

If they're not related that way, then you'll have to script it.

If you need them all to change at the same time (or all roll back in the
event of an update failure) then you can wrap the changes in a
transaction (begin/end pair) and it'll be atomic.