Thread: can I update multiple table at a time?
Hello Gurus, Is it possible to update two table using single sql statement? huh? let me be clear I have table master with state and date field. And in history table I have same fields i.e., state and date. I'm writing an update statement to set 'open' state records to 'closed' state after 7. both table can be joined using id. Any thoughts? Thanks
On Thu, 9 Oct 2008 22:12:04 -0700 (PDT) Jagadeesh <mnjagadeesh@gmail.com> wrote: > Hello Gurus, > > Is it possible to update two table using single sql statement? huh? > let me be clear > > I have table master with state and date field. And in history table I > have same fields i.e., state and date. I'm writing an update statement > to set 'open' state records to 'closed' state after 7. both table can > be joined using id. > > Any thoughts? Use a transaction? > > Thanks > -- The PostgreSQL Company since 1997: http://www.commandprompt.com/ PostgreSQL Community Conference: http://www.postgresqlconference.org/ United States PostgreSQL Association: http://www.postgresql.us/
Hi, Jagadeesh I would suggest you 2 solutions: 1. to use a function to edit these fields. That way you will be sure that you will not forget to update both tables. 2. use a trigger on master table. If I understand you - the history table could be filled using trigger, when data in master table changes. That way you could change only master table in your application. Good luck! Jagadeesh rašė: > Hello Gurus, > > Is it possible to update two table using single sql statement? huh? > let me be clear > > I have table master with state and date field. And in history table I > have same fields i.e., state and date. I'm writing an update statement > to set 'open' state records to 'closed' state after 7. both table can > be joined using id. > > Any thoughts? > > Thanks > > -- Julius Tuskenis Programavimo skyriaus vadovas UAB nSoft mob. +37068233050
Hi Julius Tuskenis, Many thanks for helping me. These are good solutions. But I have some doubts Look below On Oct 10, 11:06 am, jul...@nsoft.lt (Julius Tuskenis) wrote: > I would suggest you 2 solutions: > 1. to use a function to edit these fields. That way you will be sure > that you will not forget to update both tables. > 2. use a trigger on master table. If I understand you - the history > table could be filled using trigger, when data in master table changes. > That way you could change only master table in your application. > I think having triggers would bring performance down. So instead can we have single sql update statement to update all four fields of two tables? Just a crazy idea. > Good luck! > Thanks for the wish. These wishes needed to implement smart solution to above problem :)
I don't know a way to edit 2 tables in 1 SQL statement. If you have 2 sql statements then the performance should be almost the same with the function and maybe slightly worse with a trigger. Jagadeesh rašė: > Hi Julius Tuskenis, > > Many thanks for helping me. These are good solutions. But I have some > doubts > Look below > > On Oct 10, 11:06 am, jul...@nsoft.lt (Julius Tuskenis) wrote: > > >> I would suggest you 2 solutions: >> 1. to use a function to edit these fields. That way you will be sure >> that you will not forget to update both tables. >> 2. use a trigger on master table. If I understand you - the history >> table could be filled using trigger, when data in master table changes. >> That way you could change only master table in your application. >> >> > I think having triggers would bring performance down. So instead can > we have single > sql update statement to update all four fields of two tables? Just a > crazy idea. > > >> Good luck! >> >> > Thanks for the wish. > These wishes needed to implement smart solution to above problem :) > > -- Julius Tuskenis Programavimo skyriaus vadovas UAB nSoft mob. +37068233050
Jagadeesh wrote: > Hello Gurus, > > Is it possible to update two table using single sql statement? huh? > let me be clear > > I have table master with state and date field. And in history table I > have same fields i.e., state and date. I'm writing an update statement > to set 'open' state records to 'closed' state after 7. both table can > be joined using id. > > Any thoughts? > > Thanks > > Update a view using the rule system? See docs on rules: http://www.postgresql.org/docs/8.3/static/rules-update.html and this thread: http://forums.devshed.com/postgresql-help-21/rules-views-and-updating-multiple-tables-58288.html cheers, HH -- H. Hall ReedyRiver Group LLC http://www.reedyriver.com
If you could declare a Foreign Key on two columns in "history" table referencing "master" table, and declare this FK with "ON UPDATE CASCADE" option, then postgres will update status in history table for you whenever you update status in master table. Igor -----Original Message----- From: pgsql-admin-owner@postgresql.org [mailto:pgsql-admin-owner@postgresql.org] On Behalf Of Jagadeesh Sent: Friday, October 10, 2008 1:12 AM To: pgsql-admin@postgresql.org Subject: [ADMIN] can I update multiple table at a time? Hello Gurus, Is it possible to update two table using single sql statement? huh? let me be clear I have table master with state and date field. And in history table I have same fields i.e., state and date. I'm writing an update statement to set 'open' state records to 'closed' state after 7. both table can be joined using id. Any thoughts? Thanks -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin