So I have two table with id_pastille and I want to create a trigger that each time that one id_pastille is deleted every table that has the id_pastille is deleted as well.
CREATE TABLE tp1_trajet
(
id_trajet int NOT NULL,
longitude double precision NOT NULL,
latitude double precision NOT NULL,
"date" timestamp NOT NULL,
id_pastille int NOT NULL,
CONSTRAINT PK_zone_geographique PRIMARY KEY ( id_trajet ),
CONSTRAINT FK_ FOREIGN KEY ( id_pastille ) REFERENCES tp1_pastille ( id_pastille )
and let say this table
CREATE TABLE tp1_compte
(
id_compte int NOT NULL,
date_achat date NOT NULL,
id_pastille int NOT NULL,
id_usager int NOT NULL,
probabilite_malade double precision NULL,
CONSTRAINT PK_compte PRIMARY KEY ( id_compte ),
CONSTRAINT UQ_compte_id_pastille UNIQUE ( id_pastille ),
CONSTRAINT UQ_compte_id_usager UNIQUE ( id_usager ),
CONSTRAINT FK_compte_id_pastille FOREIGN KEY ( id_pastille ) REFERENCES tp1_pastille ( id_pastille ),
CONSTRAINT FK_compte_id_usager FOREIGN KEY ( id_usager ) REFERENCES tp1_usager ( id_usager )
);
I came up with this so far :
CREATE FUNCTION Delete_pastille() RETURNS trigger
AS $DeleteCustomerWithOrders$
BEGIN
DELETE FROM tp1_bulle,
tp1_compte,
tp1_pastille,
tp1_signes_vitaux,
tp1_usager,
tp1_transaction,
tp1_trajet
WHERE id_pastille = OLD."Id";
RETURN OLD;
END;
$DeleteCustomerWithOrders$
LANGUAGE plpgsql;