Bashkim Bekteshi
Bashkim Bekteshi

Reputation: 31

Create a trigger to run before TRUNCATE TABLE

Can I use a MySQL trigger before TRUNCATE TABLE?

I have Googled it but I can't find any answers.

P.S. I made the trigger before DELETE but when I run TRUNCATE TABLE xxx, the trigger doesn't start; it starts only when I use DELETE in a query, not TRUNCATE.

Upvotes: 0

Views: 2521

Answers (1)

Rahul
Rahul

Reputation: 77876

Did you tried reading the specific document? Triggers applies only for DML statements (INSERT | UPDATE | DELETE) and not for DDL commands (TRUNCATE).

Since you tagged both MySQL and SQL Server

Look at MySQL Documentation; create trigger syntax says

CREATE
    [DEFINER = { user | CURRENT_USER }]
    TRIGGER trigger_name
    trigger_time trigger_event
    ON tbl_name FOR EACH ROW
    trigger_body

Where trigger_event: { INSERT | UPDATE | DELETE }

Look at SQL Server Documentation; create trigger syntax says

CREATE TRIGGER [ schema_name . ]trigger_name 
ON { table | view } 
[ WITH <dml_trigger_option> [ ,...n ] ]
{ FOR | AFTER | INSTEAD OF } 
{ [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] } 

As you can see, trigger events are only INSERT | UPDATE | DELETE. It's not possible using trigger in truncate statement.

Upvotes: 4

Related Questions