Home > Database > Mysql Tutorial > mysql event的用法_MySQL

mysql event的用法_MySQL

WBOY
Release: 2016-06-01 13:39:18
Original
937 people have browsed it

bitsCN.com
mysql event的用法 MySQL从5.1开始支持event功能,类似oracle的job功能。有了这个功能之后我们就可以让MySQL自动的执行数据汇总等功能,不用像以前需要操作系统提供的定时器的支持了。如linux crontab功能。       创建测试表   CREATE TABLE t   (   v VARCHAR(100) NOT NULL   ) ENGINE INNODB DEFAULT CHARSET=utf8;   创建定时器调用的存储过程   DELIMITER $$   DROP PROCEDURE IF EXISTS e_test $$   CREATE PROCEDURE e_test()   BEGIN   INSERT INTO t VALUES('1');   END $$   DELIMITER ;   要使定时起作用 MySQL的常量GLOBAL event_scheduler必须为on或者是1       -- 查看是否开启定时器   SHOW VARIABLES LIKE '%sche%';   -- 开启定时器 0:off 1:on   SET GLOBAL event_scheduler = 1;   -- 创建事件   --每隔一秒自动调用e_test()存储过程   CREATE EVENT IF NOT EXISTS event_test   ON SCHEDULE EVERY 1 SECOND   ON COMPLETION PRESERVE   DO CALL e_test();   -- 开启事件   ALTER EVENT event_test ON   COMPLETION PRESERVE ENABLE;   -- 关闭事件   ALTER EVENT event_test ON   COMPLETION PRESERVE DISABLE;   SELECT * FROM t;   一些例子:       从现在开始每隔九天定时执行   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  bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template