Home  >  Article  >  Database  >  mysql task scheduling implementation

mysql task scheduling implementation

大家讲道理
大家讲道理Original
2016-11-12 10:00:551064browse

Today I have a business requirement, and I need to reset the serial number every day. I remembered that Oracle has a job, so I thought that Mysql should have a similar one. I found that mysql

is implemented through EVENT

CREATE EVENT [IF NOT EXISTS] event_name  
   
    ON SCHEDULE schedule  
   
    [ON COMPLETION [NOT] PRESERVE]  
   
    [ENABLE | DISABLE]  
   
    [COMMENT 'comment']  
   
    DO sql_statement;  
   
schedule:  
   
    AT TIMESTAMP [+ INTERVAL INTERVAL]  
   
| EVERY INTERVAL [STARTS TIMESTAMP] [ENDS TIMESTAMP]  
   
INTERVAL:  
   
    quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |  
   
              WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |  
   
              DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}

It is simple to use as follows

DELIMITER $$  
  /**  
     * 重置流水号  
     *   
     */  
-- SET GLOBAL event_scheduler = ON$$     -- required for event to execute but not create      
   
CREATE  /*[DEFINER = { user | CURRENT_USER }]*/ EVENT `xxx`.`reset_serialNumber`  
   
ON SCHEDULE EVERY 1 DAY STARTS '2014-05-06 23:59:59' 
     /* uncomment the example below you want to use */  
   
    -- scheduleexample 1: run once  
   
       --  AT 'YYYY-MM-DD HH:MM.SS'/CURRENT_TIMESTAMP { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] }  
   
    -- scheduleexample 2: run at intervals forever after creation  
   
       -- EVERY 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...]  
   
    -- scheduleexample 3: specified start time, end time and interval for execution  
       /*EVERY 1  [HOUR|MONTH|WEEK|DAY|MINUTE|...]  
   
       STARTS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1[HOUR|MONTH|WEEK|DAY|MINUTE|...] }  
   
       ENDS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] } */  
   
/*[ON COMPLETION [NOT] PRESERVE]  
[ENABLE | DISABLE]  
[COMMENT 'comment']*/  
   
DO  
    BEGIN 
        UPDATE xxx_sequence  
                   SET current_value = 0  
                   WHERE id = 1;  
    END$$  
   
DELIMITER ;


Statement:
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