I have two simple tables:
CREATE TABLE nene(
id integer primary key,
nombre character varying,
altaCasal integer);
CREATE TABLE actividades (
nen integer,
actividad character varying,
CONSTRAINT nen_fkey FOREIGN KEY (nen)
REFERENCES public.nene (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION);
and I want to make a trigger that when I insert something in actividades
I do a update
in nene
in the field altaActividad
indicating the number of activities to which a child is pointing, I have generated a function and its trigger
the problem is that when I do a insert
I update all the fields, how can I just update the id of the child to whom the insert is made?
CREATE OR REPLACE FUNCTION public.registre_actividades()
RETURNS trigger AS
$BODY$
declare
acti integer=0;
begin
IF (TG_OP = 'INSERT') THEN
update nene set altaCasal=(acti+1);
RETURN NEW;
END IF;
RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
CREATE TRIGGER Acti after INSERT ON actividades FOR EACH ROW EXECUTE PROCEDURE public.registre_actividades();