CREATE EVENT
Events can run once (AT) or repeatedly (EVERY). They can be enabled, disabled, or set to expire automatically.
Events can run once (AT) or repeatedly (EVERY). They can be enabled, disabled, or set to expire automatically.
-- One-time event
CREATE EVENT one_time_cleanup
ON SCHEDULE AT "2024-12-31 23:59:59"
DO TRUNCATE TABLE temp_data;
-- Recurring event every day at midnight
CREATE EVENT daily_stats
ON SCHEDULE EVERY 1 DAY
STARTS "2024-01-01 00:00:00"
DO CALL aggregate_daily_stats();
-- Recurring with expiry
CREATE EVENT weekly_purge
ON SCHEDULE EVERY 7 DAY ENDS CURRENT_TIMESTAMP + INTERVAL 1 YEAR
DO DELETE FROM audit_log WHERE created_at < NOW() - INTERVAL 90 DAY;
Always set STARTS explicitly — without it, the first run time is unpredictable.