这篇文章主要为大家展示了“mysql中event怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“mysql中event怎么用”这篇文章吧。
—— 查看event的create信息
SHOW CREATE event event_test;
——查看某个schema下的event状态
SELECT event_schema,event_name,STATUS FROM information_schema.events WHERE event_schema = 'xxxx'
——查看event功能是否开启
mysql> show variables like '%event%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
1 row in set (0.00 sec)
开启event功能,可设置为ON|1
mysql> set global event_scheduler=on;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%event%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.00 sec)
-- 创建测试表
CREATE TABLE t(X DATETIME);
-- 创建一个测试的proc,为了向测试表中插入系统时间
DELIMITER $$
DROP PROCEDURE IF EXISTS e_test $$
CREATE PROCEDURE e_test()
BEGIN
INSERT INTO t VALUES(NOW());
END $$
DELIMITER ;
-- 创建事件
--每隔十秒自动调用e_test()存储过程
CREATE EVENT IF NOT EXISTS event_test
ON SCHEDULE EVERY 10 SECOND
ON COMPLETION PRESERVE
DO CALL e_test();
过会儿select测试表中的数据,如下:
-- 开启事件
ALTER EVENT event_test ON
COMPLETION PRESERVE ENABLE;
-- 关闭事件
ALTER EVENT event_test ON
COMPLETION PRESERVE DISABLE;
一些例子:
从现在开始每隔九天定时执行
CREATE EVENT EVENT1
ON SCHEDULE EVERY 9 DAY STARTS NOW()
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL TOTAL();
END
每个月的一号凌晨1 点执行
CREATE EVENT EVENT2 ON SCHEDULE EVERY 1 MONTH STARTS
DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH),INTERVAL 1 HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL STAT();
END
每个季度一号的凌晨2点执行
CREATE EVENT TOTAL_SEASON_EVENT
ON SCHEDULE EVERY 1 QUARTER STARTS DATE_ADD(DATE_ADD(DATE( CONCAT(YEAR(CURDATE()),'-',ELT(QUARTER(CURDATE()),1,4,7,10),'-',1)),INTERVAL 1 QUARTER),INTERVAL 2 HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL SEASON_STAT();
END
每年1月1号凌晨四点执行
CREATE EVENT TOTAL_YEAR_EVENT
ON SCHEDULE EVERY 1 YEAR STARTS DATE_ADD(DATE(CONCAT(YEAR(CURDATE()) + 1,'-',1,'-',1)),INTERVAL 4 HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL YEAR_STAT();
END
以上是“mysql中event怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!