Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PostgreSQL Question: Delete after: So lets say I have two tables test1_tbl and t

ID: 3918997 • Letter: P

Question

PostgreSQL Question: Delete after:

So lets say I have two tables test1_tbl and test2_tbl :

Create table test1_tbl(id Integer Not NULL Primary Key, name text);

Create table test2_tbl(id Integer Not NULL Primary Key References test1_tbl(id), name text);

NOW, What I want to do is create a trigger that automattically deletes a record from table 1 if I delete a record from table 2

Similar to this

Delete from table2_tbl where id = 1;

(IF I RUN THIS I want something like this to execute below because of a trigger)

Delete from table1_tbl where id = 1);

Explanation / Answer

DELIMITER $$
CREATE TRIGGER tgr_1 AFTER DELETE ON table2_tbl
FOR EACH ROW
BEGIN
DELETE FROM table1_tbl where id = old.id;
END $$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote