Trigger in Postgresql

Anonymous
2023-08-30T15:21:18.0733333+00:00

Need help in creating trigger in Postgresql , if a record is inserted or updated in appoinment create a record in appointmentaudit

CREATE TABLE public.appointment (
	appid int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE),
	notes varchar(200) NULL,
	appdate date NOT NULL,
	patientid int4 NULL,
	clinicianid int4 NULL,
	statusid int4 NULL,
	CONSTRAINT pk__app PRIMARY KEY (appid)
);


-- public.appointment foreign keys

ALTER TABLE public.appointment ADD CONSTRAINT "fk_dbo.appointment_dbo.appstatus_id" FOREIGN KEY (statusid) REFERENCES public.appstatus(id);
ALTER TABLE public.appointment ADD CONSTRAINT "fk_dbo.appointment_dbo.clinician_clinicianid" FOREIGN KEY (clinicianid) REFERENCES public.clinician(clinicianid);
ALTER TABLE public.appointment ADD CONSTRAINT "fk_dbo.appointment_dbo.patient_patientid" FOREIGN KEY (patientid) REFERENCES public.patient(patientid);

CREATE TABLE public.appointmentaudit (
	appid int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE),
	notes varchar(200) NULL,
	appdate date NOT NULL,
	patientid int4 NULL,
	clinicianid int4 NULL,
	statusid int4 NULL,
	CONSTRAINT pk__appaudit PRIMARY KEY (appid)
);


-- public.appointmentaudit foreign keys

ALTER TABLE public.appointmentaudit ADD CONSTRAINT "fk_dbo.appointmentaudit_dbo.appstatus_id" FOREIGN KEY (statusid) REFERENCES public.appstatus(id);
ALTER TABLE public.appointmentaudit ADD CONSTRAINT "fk_dbo.appointmentaudit_dbo.clinician_clinicianid" FOREIGN KEY (clinicianid) REFERENCES public.clinician(clinicianid);
ALTER TABLE public.appointmentaudit ADD CONSTRAINT "fk_dbo.appointmentaudit_dbo.patient_patientid" FOREIGN KEY (patientid) REFERENCES public.patient(patientid);
Azure Database for PostgreSQL
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2023-08-31T06:39:49.7566667+00:00
    create Trigger function:
    
    SQL
    
    Copy
    CREATE OR REPLACE FUNCTION function_copy() RETURNS TRIGGER AS
    $BODY$
    BEGIN
        INSERT INTO
            public.appointmentaudit(appid,notes,appdate,patientid,clinicianid,statusid)
            VALUES(new.appid,new.notes,new.appdate,new.patientid,new.clinicianid,new.statusid);
    
               RETURN new;
    END;
    $BODY$
    language plpgsql;
    Create Trigger:
    
    SQL
    
    Copy
    CREATE TRIGGER trig_copy
         AFTER INSERT ON public.appointment
         FOR EACH ROW
         EXECUTE PROCEDURE function_copy();
    
    

  2. Anonymous
    2023-09-08T05:47:16.1166667+00:00
    
    Please check this
    https://learn.microsoft.com/en-us/answers/questions/1355461/ef-crud-insert-data-into-another-table-while-inser
    
    
    CREATE OR REPLACE FUNCTION function_copy() RETURNS TRIGGER AS
    $BODY$
    BEGIN
        INSERT INTO
            public.appointmentaudit(appid,notes,appdate,patientid,clinicianid,statusid)
            VALUES(new.appid,new.notes,new.appdate,new.patientid,new.clinicianid,new.statusid);
    
               RETURN new;
    END;
    $BODY$
    language plpgsql;
    Create Trigger:
    
    SQL
    
    Copy
    CREATE TRIGGER trig_copy
         AFTER INSERT ON public.appointment
         FOR EACH ROW
         EXECUTE PROCEDURE function_copy();
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.