SQL: PostgreSQL Question: Delete after: So lets say I have two tables test1_tbl
ID: 3919159 • Letter: S
Question
SQL: 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
CREATE TRIGGER sampleTrigger
ON test2_tbl
FOR DELETE
AS
DELETE FROM test1_tbl
WHERE ID IN(SELECT deleted.id FROM deleted)
GO
Here we will apply the trigger on the 1st table and when any record is deleted it will execute the following trigger and delete the data from the 1st table and written in the code
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.