Lajos Arpad
Lajos Arpad

Reputation: 76737

MySQL trigger to fire on alter or drop

I've encountered an interesting problem. I would like to create two triggers, trigger1 would fire on DROP TABLE and trigger2 on ALTER TABLE. I would like trigger1 and trigger2 to fire if any table is dropped or altered, but, unfortunately I don't know the syntax to do that and I couldn't find a syntax for such triggers either. In MySQL I can only write triggers which fire before/after INSERT, UPDATE, or DELETE, but now I would like to write triggers which would be applicable on database level and would fire on table events. Can somebody help me?

Upvotes: 5

Views: 5193

Answers (3)

WGR Studio
WGR Studio

Reputation: 1

PROCEDURE `pr_new_type`( IN column_name varchar(10) )
BEGIN
SET @queryText = CONCAT('ALTER TABLE `user_rights` ADD ', column_name, ' BINARY( 9 ) NULL');
PREPARE query FROM @queryText;
EXECUTE query;
DEALLOCATE PREPARE query;
END

test this

Upvotes: 0

John P
John P

Reputation: 15245

It's not possible to use a trigger for drop statements.According to the documentation the trigger is activated on

  • INSERT: The trigger is activated whenever a new row is inserted into the table; for example, through INSERT, LOAD DATA, and REPLACE statements.

    UPDATE: The trigger is activated whenever a row is modified; for example, through UPDATE statements.

    DELETE: The trigger is activated whenever a row is deleted from the table; for example, through DELETE and REPLACE statements. However, DROP TABLE and TRUNCATE TABLE statements on the table do not activate this trigger, because they do not use DELETE.

It doesn't say anything about alter table, but I would expect only DML-statements to be able to fire a trigger.

Upvotes: 1

gbn
gbn

Reputation: 432421

What you are looking for is known as "DDL Triggers". MySQL does not support them

There is this SourceForge request but I'm not sure how serious anyone is about adding it

Upvotes: 12

Related Questions