Events
Learn how MySQL Events let the database run SQL automatically on a schedule, like a built-in cron job.
Introduction
Triggers react to changes as they happen. But sometimes you want MySQL to run a task on its own, at a specific time or on a repeating interval — without waiting for any INSERT, UPDATE, or DELETE to happen first. That is what MySQL Events are for.
An Event is essentially a scheduled task stored inside the database itself, similar to a cron job on an operating system, except it lives in MySQL and runs SQL directly. This lesson shows you how to enable, create, and manage events.
- What a MySQL Event is and how it differs from a trigger.
- How to turn on the event scheduler.
- How to create a one-time or recurring event with CREATE EVENT.
- A worked example that automatically deletes old log records every day.
- How to view, disable, and drop events.
What is a MySQL Event?
A MySQL Event is a named, scheduled block of SQL that the database server runs automatically at a set time or on a repeating interval — think of it as a cron job built directly into MySQL, requiring no external scheduler or script.
| Feature | Trigger | Event |
|---|---|---|
| Fires when... | A row is inserted, updated, or deleted | A scheduled time arrives |
| Tied to | A specific table | The database server’s clock |
| Typical use | Auditing, validation | Cleanup jobs, scheduled reports, periodic updates |
Events are managed by a background process inside MySQL called the event scheduler, which must be turned on before any event will actually run.
Enabling the Event Scheduler
By default, the event scheduler is often turned off. You can check its status, and turn it on, with the following statements.
SHOW VARIABLES LIKE 'event_scheduler';
SET GLOBAL event_scheduler = ON;+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| event_scheduler | ON |
+------------------+-------+SET GLOBAL event_scheduler = ON; only lasts until MySQL restarts. To make it permanent, add event_scheduler = ON to your MySQL configuration file (my.cnf or my.ini).
Creating an Event
An event is created with CREATE EVENT, naming a schedule with ON SCHEDULE, and the SQL to run with DO.
CREATE EVENT event_name
ON SCHEDULE schedule_expression
DO
-- SQL statement(s) to run
;The schedule_expression tells MySQL exactly when, and how often, to run the event body.
One-Time vs Recurring Events
An event can run exactly once at a specific moment (AT), or repeatedly on an interval (EVERY).
-- Runs a single time, at a specific timestamp
CREATE EVENT one_time_cleanup
ON SCHEDULE AT '2026-08-01 00:00:00'
DO
DELETE FROM temp_sessions WHERE expired = 1;-- Runs forever, once every day, starting now
CREATE EVENT daily_cleanup
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP
DO
DELETE FROM temp_sessions WHERE expired = 1;EVERY accepts any interval, such as EVERY 1 HOUR, EVERY 30 MINUTE, or EVERY 1 WEEK. STARTS controls when the first run happens, and an optional ENDS can give a recurring event a final cutoff date.
Worked Example: Deleting Old Logs
A common real use of events is automatic cleanup: deleting log records that have grown old enough that nobody needs them anymore.
CREATE TABLE logs (
id INT AUTO_INCREMENT PRIMARY KEY,
message VARCHAR(255),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);Now create an event that runs once every day and removes any log row older than 30 days.
CREATE EVENT delete_old_logs
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP
DO
DELETE FROM logs
WHERE created_at < NOW() - INTERVAL 30 DAY;From now on, once every 24 hours, MySQL will automatically run this DELETE statement for you — no cron job, script, or application code needed to keep the logs table trimmed.
Viewing, Altering, and Dropping Events
You can list every event in the current database, temporarily disable one, or remove it entirely.
SHOW EVENTS;
ALTER EVENT delete_old_logs DISABLE;
ALTER EVENT delete_old_logs ENABLE;
DROP EVENT delete_old_logs;+----------------+----------------+-----------+---------------+--------+
| Db | Name | Definer | Type | Status |
+----------------+----------------+-----------+---------------+--------+
| company_db | delete_old_logs| root@host | RECURRING | ENABLED|
+----------------+----------------+-----------+---------------+--------+SHOW EVENTS is useful for confirming an event actually exists, checking whether it is currently ENABLED or DISABLED, and seeing whether it is a ONE TIME or RECURRING event.
Common Mistakes
- Creating an event without first enabling the event scheduler — it will simply never run.
- Forgetting that SET GLOBAL event_scheduler = ON; resets after a server restart.
- Using AT for something that should repeat, or EVERY for something that should only happen once.
- Writing an event that deletes data without first testing the WHERE clause using a plain SELECT.
- Forgetting an event exists, since like triggers, it never appears anywhere in application code.
Best Practices
- Always test the underlying DELETE, UPDATE, or INSERT logic manually before wrapping it in an event.
- Set event_scheduler = ON in your permanent MySQL configuration file, not just with SET GLOBAL.
- Name events descriptively, such as delete_old_logs or send_daily_summary.
- Use SHOW EVENTS periodically to confirm scheduled jobs are still ENABLED.
- Keep a written record of every event and what it does, since they are easy for a team to lose track of.
Frequently Asked Questions
Do I need special privileges to create events?
Yes, creating and managing events requires the EVENT privilege on the relevant database.
What happens to a ONE TIME event after it runs?
By default, MySQL automatically drops a completed one-time event unless it was created with ON COMPLETION PRESERVE.
Can an event call a stored procedure?
Yes. The DO clause can contain a CALL statement, letting an event trigger more complex, reusable logic.
Is an event the same thing as a trigger?
No. A trigger fires in response to a data change on a specific table; an event fires based purely on the clock, regardless of whether any data changed.
Key Takeaways
- A MySQL Event is a scheduled task the database runs automatically, like a built-in cron job.
- The event scheduler must be turned on with SET GLOBAL event_scheduler = ON; before any event runs.
- CREATE EVENT combines a name, an ON SCHEDULE clause, and a DO clause containing the SQL to run.
- AT schedules a single run; EVERY schedules a repeating interval.
- SHOW EVENTS lists existing events; ALTER EVENT and DROP EVENT manage them.
Summary
Events give MySQL the ability to run SQL on its own schedule, which is perfect for routine maintenance tasks like cleaning up old data, without relying on an external scheduler.
In this lesson, you learned what an event is, how to enable the event scheduler, how to create both one-time and recurring events, and how to manage them with SHOW, ALTER, and DROP. Next, you will learn about Transactions — how to group multiple SQL statements into a single, safe unit of work.
- You understand what a MySQL Event is.
- You can enable the event scheduler.
- You can create both one-time and recurring events.
- You are ready to learn about Transactions.