如何解决Postgres更新不同的表
你好,我试图创建一个触发器,在插入特定行的特定列后,trgigger在特定位置的另一个表上添加了该值。 我有一个称为更新的表:
create table public.updates (
id serial not null,matricula varchar(10) NOT NULL,primensalidade int,segmensalidade int,tercmensalidade int,quamensalidade int,quimensalidade int,sexmensalidade int,foreign key (id) references creditos(id));
我有一个名为:
的表CREATE TABLE public.creditos (
id serial NOT NULL,veiculo varchar(10) NOT NULL,nome bpchar(30) NOT NULL,telemovel varchar(10) NOT NULL,valordevendapronto int4 NOT NULL,juros int4 NULL,valortotal int4 NULL,entradainicial int4 NOT NULL,primensalidade int4 NULL,segmensalidade int4 NULL,tercmensalidade int4 NULL,quamensalidade int4 NULL,quimensalidade int4 NULL,sexmensalidade int4 NULL,datainicial date NULL DEFAULT now(),datafinal date NULL,pago bpchar(3) NULL,CONSTRAINT creditos_pkey PRIMARY KEY (id));
我的目标是在我插入updates.primensalidade之后,将其转到creditos.primensalidade,并将其更改为在updates.primensalidade之前我选择的位置,其中我的creditos.matricula和updates.matricula相同。 我到目前为止还很:
CREATE OR REPLACE function creditoupdate()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
begin
insert into creditos(primensalidade) values(new.primensalidade) where matricula = new.matricula ;
return new;
end;
$function$
;
create trigger creditoupdatetrigger after
insert
on
updates for each row execute function creditoupdate();
我希望你能帮助我。
解决方法
WHERE
不能在INSERT
中这样使用。
要更新表primensalidade
中的creditos
列,可以使用以下内容:
UPDATE creditos SET primensalidade = new.primensalidade where matricula = new.matricula;
有关UPDATE的更多信息-https://www.postgresql.org/docs/current/sql-update.html
有关INSERT的更多信息-https://www.postgresql.org/docs/current/sql-insert.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。