Postgres 更新了一个不同的 table
Postgres update a diferent table
你好,我正在尝试创建一个触发器,在某一行的某一列上插入后,触发器会将该值添加到另一个 table 的某个位置。
我有一个 table 叫做更新:
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));
我有一个 table 叫:
CREATE TABLE public.creditos (
id serial NOT NULL,
veiculo varchar(10) NOT NULL,
matricula 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));
而我的 objective 是在我插入 updates.primensalidade 之后,它转到 creditos.primensalidade 并将其更改为我之前在 updates.primensalidade 上选择的内容,其中我的 [=25] =] 和 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
中使用。
要更新 table creditos
中的 primensalidade
列,可以使用以下内容:
UPDATE creditos SET primensalidade = new.primensalidade where matricula = new.matricula;
关于更新的更多信息 - https://www.postgresql.org/docs/current/sql-update.html
关于 INSERT 的更多信息 - https://www.postgresql.org/docs/current/sql-insert.html
你好,我正在尝试创建一个触发器,在某一行的某一列上插入后,触发器会将该值添加到另一个 table 的某个位置。 我有一个 table 叫做更新:
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));
我有一个 table 叫:
CREATE TABLE public.creditos (
id serial NOT NULL,
veiculo varchar(10) NOT NULL,
matricula 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));
而我的 objective 是在我插入 updates.primensalidade 之后,它转到 creditos.primensalidade 并将其更改为我之前在 updates.primensalidade 上选择的内容,其中我的 [=25] =] 和 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
中使用。
要更新 table creditos
中的 primensalidade
列,可以使用以下内容:
UPDATE creditos SET primensalidade = new.primensalidade where matricula = new.matricula;
关于更新的更多信息 - https://www.postgresql.org/docs/current/sql-update.html
关于 INSERT 的更多信息 - https://www.postgresql.org/docs/current/sql-insert.html