响应@Zxaos的请求,由于我们没有MySQL触发器的AND / OR运算符,因此从您的代码开始,下面是一个完整的示例,以实现相同的效果。
1.定义INSERT触发器:
DELIMITER //
DROP TRIGGER IF EXISTS my_insert_trigger//
CREATE DEFINER=root@localhost TRIGGER my_insert_trigger
AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
-- Call the common procedure ran if there is an INSERT or UPDATE on `table`
-- NEW.id is an example parameter passed to the procedure but is not required
-- if you do not need to pass anything to your procedure.
CALL procedure_to_run_processes_due_to_changes_on_table(NEW.id);
END//
DELIMITER ;
2.定义UPDATE触发器
DELIMITER //
DROP TRIGGER IF EXISTS my_update_trigger//
CREATE DEFINER=root@localhost TRIGGER my_update_trigger
AFTER UPDATE ON `table`
FOR EACH ROW
BEGIN
-- Call the common procedure ran if there is an INSERT or UPDATE on `table`
CALL procedure_to_run_processes_due_to_changes_on_table(NEW.id);
END//
DELIMITER ;
3.定义这两个触发器使用的通用PROCEDURE:
DELIMITER //
DROP PROCEDURE IF EXISTS procedure_to_run_processes_due_to_changes_on_table//
CREATE DEFINER=root@localhost PROCEDURE procedure_to_run_processes_due_to_changes_on_table(IN table_row_id VARCHAR(255))
READS SQL DATA
BEGIN
-- Write your MySQL code to perform when a `table` row is inserted or updated here
END//
DELIMITER ;
您注意到,在完成定义触发器和过程的业务后,我会小心地恢复定界符。