简体   繁体   中英

update two tables at the same time postgresql

I have a problem in POSTGRESQL.

I want to update two tables at the same time. Both update can be applied since they have the same condition.

Here's the individual update:

UPDATE standards.standards
SET description = 'The student will demonstrate positive self esteem.'
WHERE description LIKE 'The student will demonstrate positive his/her self esteem.' 
AND custom_code LIKE 'qwertyuiop';

UPDATE bank SET description = 'The student will demonstrate positive self esteem.'
WHERE description LIKE 'The student will demonstrate positive his/her self esteem.'
AND designation LIKE 'asdfghjkl';

I want to update both in just one sql statement. Here's something I did but got an error since POSTGRESQL does not allow update of two tables at the same time.

UPDATE standards.standards ss, bank bb
SET description = 'The student will demonstrate positive self esteem.'
WHERE description LIKE 'The student will demonstrate positive his/her self esteem.'
AND (ss.custom_code LIKE 'qwertyuiop' OR bb.designation LIKE 'asdfghjkl');

Can you help me on this? I want just one sql statement. Thanks!

There's no point in doing this, since you will not make it happen atomically anyway. It's not going to solve any problem that couldn't be solved in a better way (eg using a parameter for the values if you don't want to send them twice).

You can update two tables at once using a view, for example. But again, it's not going to be atomic. You still need proper transaction handling to make sure this works as intended.

You can write a FUCTION for this, Am considering your current info. as an example to demonstrate

create table standards (description text,custom_code text);
insert into standards 
values ('hai','AA'),
       ('how are you ?','AA'),
       ('The student will demonstrate positive his/her self esteem.','qwertyuiop');


create table bank (description text,designation text);
insert into bank 
values ('Am','BB'),
       ('Fine','BB'),
       ( 'The student will demonstrate positive his/her self esteem.' ,'asdfghjkl');

and create a Fucntion like this

create or replace function update_tables(_desc text, /*-- your value to update i.e The student will demonstrate positive self esteem.*/
                                         _WDesc text,  /*-- your value to use in where clause  i.e 'The student will demonstrate positive his/her self esteem.' */
                                         _custom_code text, /* this AND custom_code LIKE 'qwertyuiop'; goes here */
                                         _designation text /* this AND designation LIKE 'asdfghjkl'; goes here*/
                                         ) 
returns void as 
/* add two update queries inside this function */
/* 1 Updating  table standards*/
'update standards 
 set description = _desc
 where description like  _WDesc and 
        custom_code like _custom_code;'
/*End*/
/* 2 Updating  table bank*/
'update bank 
 set description = _desc 
 where description like  _WDesc and 
      designation like _designation;'
/*End*/
language sql

and here is your one sql command to update both tables

select update_tables ('The student will demonstrate positive self esteem.',
                      'The student will demonstrate positive his/her self esteem.',
                      'qwertyuiop',
                      'asdfghjkl')

sqlfiddle

There is a point of doing this if your tables (2 or more) are linked together by foreign keys and you want to update this foreign key on all of them at the same time without getting "violates foreign key constraint" error.

To do so your tables must be created with " ON UPDATE CASCADE " option or you can alter them if they are already populated with this command:

ALTER TABLE child_table
   DROP CONSTRAINT child_table_myField_fkey,
   ADD CONSTRAINT child_table_myField_fkey FOREIGN KEY (myField, ...)
      REFERENCES mother_table(myField, ...) ON UPDATE CASCADE;

From there you can update your mother_table directly and child_table(s) will be updated in the background at the same time, magic!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM