Skip to content

This guide covers how to automate SQL commands in MySQL. You’ll usually want to do this to schedule routine tasks like data backups, cleanup and reporting during off-peak hours.

What are MySQL Events?

MySQL’s event scheduler is a built-in feature for executing tasks at scheduled times. It’s similar to cron jobs in Unix/Linux systems.

Enabling the Event Scheduler

Before creating events, ensure the event scheduler is enabled:

SET GLOBAL event_scheduler = ON;

Alternatively, check its status:

SHOW VARIABLES LIKE 'event_scheduler';

How to Create a Basic Event in MySQL

You can create an event with the following syntax:

CREATE EVENT my_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
  UPDATE my_table SET my_column = my_value;

This event updates my_table one hour from the current time.

Advanced Scheduling

For more complex scheduling, you can use a couple clauses in the ON SCHEDULE section.

Repeating Events

To repeat an event, use the EVERY clause:

CREATE EVENT my_repeating_event
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP
DO
  INSERT INTO my_table (my_column) VALUES (my_value);

This will insert a new record into my_table every day.

Specifying Start and End Times

Control when an event starts and optionally when it ends:

CREATE EVENT my_timed_event
ON SCHEDULE EVERY 1 WEEK
STARTS '2023-01-01 00:00:00'
ENDS '2023-12-31 23:59:59'
DO
  CALL my_procedure();

This calls my_procedure weekly throughout 2023.

Managing Events

Viewing Existing Events

List all events:

SHOW EVENTS;

Altering Events

Modify an event’s definition:

ALTER EVENT my_event
ON SCHEDULE EVERY 2 DAYS;

Deleting Events

Remove an event when it’s no longer needed:

DROP EVENT IF EXISTS my_event;

Basedash is built as an AI-native BI platform, so teams can go from ad hoc SQL to trusted answers and dashboards quickly, without the overhead of traditional BI setup.

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.