In databases you usually want to make an action on unwanted events, so for example you are able to rollback insert if data is incorrect, but table constraints do allow such data.
I would recommend using simple BEFORE DELETE trigger:
DELIMITER $$
CREATE TRIGGER before_tabName_row_delete
BEFORE DELETE ON tabName
FOR EACH ROW
BEGIN
-- action, for example
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Deleting from tabName is forbidden!';
END$$
DELIMITER ;
In MySQL we can use SIGNAL, here is manual for it, where second block of code may tell you a bit more: https://dev.mysql.com/doc/refman/8.0/en/signal.html
45000 code stands for "unknown error" or "not handled".
When you write some bigger triggers you should be sure to handle properly all known (for your case) exceptions to avoid "some strange things in database", but here 45000 is fine, but also you could use defined by yourself error code consistent to whole database (for example to use 42000 for deletion prevention, 42001 for update prevention and 42002 for insert prevention etc.).
Usually database has its own system for these operations, so for example Oracle and MySQL have OLD.attribute and NEW.attribute values for triggers that use FOR EACH ROW
.
These values may be overwritten, or the whole transaction may be rollbacked by simple ROLLBACK
or raising exception.
Usually it is better to raise exception with its code and description so user (and some logging system' admin) will know what happened ;).
UPDATE
?DELETE
rather than toUPDATE
.