I want to protect some rows from deletion and I prefer to do it using triggers rather than logic of my application. I am using MySQL database.
What I came up with is this:
DELIMITER $$
DROP TRIGGER `preserve_permissions`$$
USE `systemzarzadzaniareporterami`$$
CREATE TRIGGER `preserve_permissions`
AFTER DELETE ON `permissions`
FOR EACH ROW
BEGIN
IF old.`userLevel` = 0 AND old.`permissionCode` = 164 THEN
INSERT INTO `permissions` SET `userLevel`=0, `permissionCode`=164;
END IF;
END$$
DELIMITER ;
But it gives me an error when I use delete:
DELETE FROM `systemzarzadzaniareporterami`.`permissions`
WHERE `userLevel` = 0 AND `permissionCode` = 164;
Error Code: 1442. Can't update table 'permissions' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Is there another way to do such a thing?