In this specific case I'm using a SQLite database, but I believe this to be more of a conceptual question with practical implications.
I have a nested-set model in my database and I would like to ensure than when a parent node is somehow deleted from it, all of its children will be deleted as well.
From SQLite's documentation, triggers allow for multiple statements, so I got something like this:
CREATE TABLE "tbl_nsm" (
"lft" INTEGER,
"rgt" INTEGER
);
CREATE TRIGGER IF NOT EXISTS "delete_entire_leaf" AFTER DELETE ON tbl_nsm
BEGIN
DELETE FROM tbl_nsm
WHERE 1=1
AND lft > OLD.lft
AND lft < OLD.rgt;
UPDATE tbl_nsm SET rgt = rgt - (OLD.rgt - OLD.lft + 1) WHERE rgt > OLD.rgt;
UPDATE tbl_nsm SET lft = lft - (OLD.rgt - OLD.lft + 1) WHERE lft > OLD.rgt;
END
;
However, when populating the table with consistent values, whenever I delete a record, the trigger seems to do nothing: all other values remain and there isn't even a single warning. Instead of an AFTER DELETE
, I changed it to a BEFORE DELETE
trigger, but to no avail.
That got me thinking: can you use DELETE
triggers to delete further values from your table? Or does SQL simply ignore any changes to the target table of such a trigger?