MYSQL定时任务 触发器

时间:2020-12-23 07:54:49

流程:

  • 查看时间调度器是否开启,OFF关闭,ON开启

SHOW VARIABLES LIKE 'event_scheduler' 

  • 设置开启

set GLOBAL event_scheduler = 1;

  • 设置分隔符为 '$$' ,mysql默认的语句分隔符为 ';' ,这样在后续的 create 到 end 这段代码都会看成是一条语句来执行

DELIMITER $$
DROP EVENT IF EXISTS event_TEST;
CREATE EVENT event_TEST
ON SCHEDULE EVERY 1 SECOND  我试的是每秒,天 DAY
STARTS '2017-04-21 10:40:00'
ENABLE
DO
BEGIN
INSERT INTO time VALUES (NOW()); 执行的SQL
END
$$
DELIMITER ;

结束:

 

触发器:insert update、delete (DML)after、begin(触发时机)

例:

CREATE TRIGGER `newStudent` AFTER INSERT ON `student` FOR EACH ROW begin
declare c int;
set c = (select stuCount from teacher t where t.id=new.t_id);
update teacher t set stuCount = c+1 where t.id = new.t_id;
end;

DROP EVENT IF EXISTS event_TEST;