mysql怎么删除触发器

删除 MySQL 触发器是相对简单的,只需要按照下面的步骤操作即可。

步骤一:查看所有的触发器

首先需要确认要删除的触发器的名称,使用如下 SQL 语句查询所有的触发器:

SHOW TRIGGERS;

这会输出所有已定义的触发器列表,如下所示:

+-------------+--------+------------+----------------+--------+----------------------+----------------------+--------+---------------+---------+--------+------+---------------------+---------------------+---------------------+

| Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer| character_set_client | collation| Database | Type | Action_condition | Action_statement | Action_orientation |

+-------------+--------+------------+----------------+--------+----------------------+----------------------+--------+---------------+---------+--------+------+---------------------+---------------------+---------------------+

| ai_orders | AFTER | orders | INSERT | FOR | 2020-03-20 14:33:05 | STRICT_TRANS_TABLES | root@% | utf8mb4 | utf8mb4_general_ci | test | FOR | NULL | BEGIN | 1 |

| bi_orders | BEFORE | orders | INSERT | FOR | 2020-03-20 14:33:05 | STRICT_TRANS_TABLES | root@% | utf8mb4 | utf8mb4_general_ci | test | FOR | deleted IS NULL | SET NEW.created = NOW(), NEW.updated = NOW() | 1 |

| bu_orders | BEFORE | orders | UPDATE | FOR | 2020-03-20 14:33:05 | STRICT_TRANS_TABLES | root@% | utf8mb4 | utf8mb4_general_ci | test | FOR | deleted IS NULL | SET NEW.updated = NOW() WHERE ORDER_ID = OLD.ORDER_ID | 1 |

+-------------+--------+------------+----------------+--------+----------------------+----------------------+--------+---------------+---------+--------+------+---------------------+---------------------+---------------------+

步骤二:删除指定的触发器

根据触发器名删除触发器:

DROP TRIGGER trigger_name;

例如,如果要删除名称为“ai_orders”的触发器,使用以下 SQL 语句:

DROP TRIGGER ai_orders;

该语句将删除名称为 "ai_orders" 的唯一触发器。

如果要删除所有的触发器,则需要执行多个 DROP TRIGGER 语句。

步骤三:验证触发器是否已删除

要验证触发器是否成功删除,可以再次运行 SHOW TRIGGERS 语句并检查是否存在已删除的触发器。

现在您已经知道了如何删除 MySQL 中的触发器!

数据库标签