创建一个在TRUNCATE TABLE之前运行的触发器

时间:2014-09-16 00:10:40

标签: mysql sql

我可以在TRUNCATE TABLE之前使用MySQL触发器吗?

我用Google搜索了但我找不到任何答案。

P.S。我在DELETE之前触发了触发器,但是当我运行TRUNCATE TABLE xxx时,触发器没有启动;它仅在我在查询中使用DELETE时开始,而不是TRUNCATE

1 个答案:

答案 0 :(得分:4)

您是否尝试过阅读特定文件?触发器仅适用于DML语句(INSERT | UPDATE | DELETE),不适用于DDL命令(TRUNCATE)。

由于您标记了MySQLSQL Server

MySQL Documentation; create trigger语法说

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 }

SQL Server Documentation; create trigger语法说

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

如您所见,trigger个事件仅为INSERT | UPDATE | DELETE。 在truncate语句中使用触发器是不可能的。

相关问题