同时更新两个表postgresql

时间:2015-01-13 07:01:10

标签: sql postgresql

我在POSTGRESQL中遇到问题。

我想同时更新两个表。两种更新都可以应用,因为它们具有相同的条件。

以下是个人更新:

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';

我想在一个sql语句中更新两者。这是我做的事情,但由于POSTGRESQL不允许同时更新两个表,因此出现了错误。

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');
你能帮我解决这个问题吗?我只想要一个sql语句。谢谢!

3 个答案:

答案 0 :(得分:3)

这样做是没有意义的,因为无论如何你都不会以原子方式实现它。它不会解决任何无法以更好的方式解决的问题(例如,如果您不想发送两次,请使用值的参数)。

例如,您可以使用视图一次更新两个表。但同样,它不会是原子的。您仍然需要正确的事务处理以确保其按预期工作。

答案 1 :(得分:1)

你可以为此写一个 FUCTION ,我会考虑你当前的信息。作为演示的例子

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');

并像这样创建一个 Fucntion

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

这是你的一个 sql命令来更新两个表

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

sqlfiddle

答案 2 :(得分:0)

如果您的表(2个或更多)通过外键链接在一起并且您希望同时更新所有这些外键而不会出现"违反外键约束& #34;错误。

为此,您的表格必须使用" ON UPDATE CASCADE"选项,或者如果已使用此命令填充它们,则可以更改它们:

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;

从那里你可以直接更新你的mother_table,child_table将同时在后台更新,魔术!

相关问题