DELIMITER $
CREATE TRIGGER bi_t1
BEFORE INSERT ON t1
FOR EACH ROW
BEGIN
IF new.m1 < 1 THEN
SET new.m1 = 1;
ELSEIF new.m1 > 10 THEN
SET new.m1 = 10;
END IF;
END $
DELIMITER ;
CREATE EVENT 事件名
ON SCHEDULE
{
AT 某个确定的时间点 |
EVERY 期望的时间间隔 [STARTS 开始日期和时间] [END 结束日期和时间]
}
DO
BEGIN
具体的语句
END
事件支持两种执行方式:在某个确定的时间点执行;每隔一段时间执行一次。
在某个确定的时间点执行,比如:
CREATE EVENT insert_t1_event
ON SCHEDULE
AT '2021-09-04 15:48:54'
DO
BEGIN
INSERT INTO t1(m1, n1) VALUES(6, 'f');
END
也可以通过表达式来指定时间点,比如:
CREATE EVENT insert_t1_event
ON SCHEDULE
AT DATE_ADD(NOW(), INTERVAL 2 DAY)
DO
BEGIN
INSERT INTO t1(m1, n1) VALUES(6, 'f');
END
每隔一段时间执行一次,比如:
CREATE EVENT insert_t1_event
ON SCHEDULE
EVERY 1 HOUR
DO
BEGIN
INSERT INTO t1(m1, n1) VALUES(6, 'f');
END
还可以指定重复执行的起始时间和结束时间,比如:
CREATE EVENT insert_t1_event
ON SCHEDULE
EVERY 1 HOUR STARTS '2021-09-04 15:48:54' ENDS '2021-09-16 15:48:54'
DO
BEGIN
INSERT INTO t1(m1, n1) VALUES(6, 'f');
END