Create a Trigger in mysql

First we need to know What is trigger:

A trigger is defined to activate when a statement inserts, updates, or deletes rows in the associated table. These row operations are trigger events. For example, rows can be inserted by INSERT or LOAD DATA statements, and an insert trigger activates for each inserted row. A trigger can be set to activate either before or after the trigger event. For example, you can have a trigger activate before each row that is inserted into a table or after each row that is updated.

Here is a simple example that associates a trigger with a table, to activate for DELETE operations.

1
2
3
4
5
6
7
DELIMITER $$
create trigger delete_user AFTER DELETE on users
FOR EACH ROW
BEGIN
Delete from address where uid=OLD.id;
END$$
DELIMITER ;

Leave a Reply