Mariadb Database level trigger

141 Views Asked by At
CREATE TRIGGER safety_drop
ON DATABASE 
FOR DROP_TABLE
AS
BEGIN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'DELETING NOT ALLOWED';
ROLLBACK;
END

ERROR

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ON DATABASE FOR DROP_TABLE AS BEGIN SIGNAL SQLSTATE '45000' SET MESSAGE_TEX' at line 2

1

There are 1 best solutions below

1
GMB On BEST ANSWER

MariaDB just does not support "database-level" triggers. Quoting the documentation (emphasis added):

A trigger, as its name suggests, is a set of statements that run, or are triggered, when an event occurs on a table. [...] The event can be an INSERT, an UPDATE or a DELETE.

So basically this works on DML operations (statements that change the data), not on DDL (operations that change the structure).

As for what you ask for, using permissions and roles to prevent users from dropping any table in the database seems like a more relevant approach.