Mysql 简明教程
MySQL - DROP TRIGGER
MySQL 中的触发器类似于过程,是一些存储程序。它们可以在与事件关联的表、架构、视图和数据库上创建,并且每当事件发生时便会调用相应的触发器。
Triggers in MySQL are stored programs similar to procedures. These can be created on a table, schema, view and database that are associated with an event and whenever an event occurs the respective trigger is invoked.
在实际上,触发器编写为对以下任一事件响应而执行:
Triggers are, in fact, written to be executed in response to any of the following events −
-
A database manipulation (DML) statement (DELETE, INSERT, or UPDATE)
-
A database definition (DDL) statement (CREATE, ALTER, or DROP).
-
A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
您可以使用 DROP TRIGGER 语句删除触发器。
You can delete a trigger using the DROP TRIGGER Statement.
Dropping Trigger in MySQL
MySQL 中的 DROP TRIGGER 语句将从数据库中删除触发器及其所有信息。
The DROP TRIGGER statement in MySQL will drop a trigger from a database, and all its information.
Syntax
以下是 MySQL DROP TRIGGER 语句的语法。
Following is the syntax of the MySQL DELETE TRIGGER Statement.
DROP TRIGGER [IF EXISTS] trigger_name
其中,trigger_name 是您需要删除的触发器的名称。
Where, trigger_name is the name of the trigger you need to delete.
Example
假设我们创建了一个名为 student 的表,如下所示:
Assume we have created a table with name student as shown below −
CREATE TABLE STUDENT(
Name varchar(35),
Age INT,
Score INT
);
以下查询在此表上创建了一个名为 sample_trigger 的触发器。如果您输入的值小于 0,则此触发器会将分数值设置为 0 分。
Following query creates a trigger named sample_trigger on this table. This trigger will set the score value 0 if you enter a value that is less than 0 as score.
DELIMITER //
CREATE TRIGGER sample_trigger
BEFORE INSERT ON STUDENT
FOR EACH ROW
BEGIN
IF NEW.score < 0 THEN SET NEW.score = 0;
END IF;
END //
DELIMITER ;
现在,让我们使用以下查询来删除我们在上一步中创建的触发器:
Now, let us use the following query to drop the trigger we created in the previous step −
DROP TRIGGER sample_trigger;
With IF EXISTS clause
如果您尝试删除不存在的触发器,将会生成如下所示的错误:
If you try to drop a trigger that doesn’t exist an error will be generated as shown below −
DROP TRIGGER demo;
输出如下:
Following is the output −
ERROR 1360 (HY000): Trigger does not exist
如果您按照下面所示,将 IF EXISTS 子句与 DROP TRIGEGR 语句一起使用,则将删除指定的触发器;如果具有给定名称的触发器不存在,则将忽略此查询。
If you use the IF EXISTS clause along with the DROP TRIGEGR statement as shown below, the specified trigger will be dropped and if a trigger with the given name, doesn’t exist the query will be ignored.
DROP TRIGGER IF EXISTS demo;