MySQL Event Timing – Adding Time to Weekly Events

MySQL

I have this event that runs every Thursday

CREATE EVENT le_drop_database
ON SCHEDULE
  EVERY 1 WEEK
STARTS CURRENT_DATE + INTERVAL 3 - WEEKDAY(CURRENT_DATE) DAY   
 DO BEGIN
  drop database carbon;
    END */$$
DELIMITER ;

I was wondering how i would add some parameter that indicates the time this event should run on Thursday for example run at 11:00 a.m

Best Answer

There are three things you should do with this

  1. Change DROP DATABASE to DROP DATABASE IF EXISTS
  2. Add INTERVAL 11 HOUR to the START (as mentioned by @ypercube)
  3. Add ON COMPLETION PRESERVE to make it a repeatable event so you don't have to create it again.

Here are the changes

DELIMITER $$    
CREATE EVENT le_drop_database
ON SCHEDULE
  EVERY 1 WEEK
STARTS CURRENT_DATE + INTERVAL 3 - WEEKDAY(CURRENT_DATE) DAY + INTERVAL 11 HOUR DO
ON COMPLETION PRESERVE
BEGIN
  DROP DATABASE IF EXISTS carbon;
END $$
DELIMITER ;

I hope this event is not in the carbon database

If you ever need to disable the event, just run

ALTER EVENT le_drop_database DISABLE;

to reenable

ALTER EVENT le_drop_database ENABLE;

CAVEAT

Please make sure you have this in my.cnf

[mysqld]
event_scheduler=1

You can enable this without restarting mysql by running

mysql> SET GLOBAL event_scheduler = 1;

Give it a Try !!!