要删除触发器,请使用DROP命令。语法如下 −
DROP TRIGGER IF EXISTS yourTriggerName;
为了理解上述语法,您需要在当前数据库中有一个触发器。
要检查触发器是否存在,您可以使用以下查询。我们的数据库中有一个触发器−
mysql> show triggers;
以下是输出 −
+-------------+--------+---------------+------------------------------------------------------------------------+--------+------------------------+--------------------------------------------+---------+----------------------+----------------------+--------------------+
| Trigger | Event | Table | Statement | Timing |Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation |
+-------------+--------+---------------+------------------------------------------------------------------------+--------+------------------------+--------------------------------------------+---------+----------------------+----------------------+--------------------+
| CheckSalary | INSERT | employeetable | if new.EmployeeSalary < 1000 then setnew.EmployeeSalary = 10000;end if | BEFORE | 2018-12-31 17:33:29.54 |STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | root@% | utf8 |utf8_general_ci | utf8mb4_0900_ai_ci |
+-------------+--------+---------------+------------------------------------------------------------------------+--------+------------------------+--------------------------------------------+---------+----------------------+----------------------+--------------------+
1 row in set (0.17 sec)
在这里,我们有一个名为'CheckSalary'的触发器在employeetable上。使用DROP命令删除触发器'CheckSalary'。查询如下 -
mysql> drop trigger if exists CheckSalary;
Query OK, 0 rows affected (0.30 sec)
使用show triggers命令检查触发器是否存在。查询如下 −
mysql> show triggers;
Empty set (0.00 sec)
现在看上面的结果,触发器不存在于数据库“test”中。我们使用 drop 将其删除。