Close

2023-07-20

Step-by-Step Guide to Creating Scheduled Events in MySQL

Step-by-Step Guide to Creating Scheduled Events in MySQL

We can create scheduled events by SQL on MySQL using the CREATE EVENT statement. The syntax for the CREATE EVENT statement is as follows:

CREATE EVENT event_name
ON SCHEDULE schedule
DO event_body;

The event_name parameter is the name of the event. The schedule parameter specifies when the event will be executed. The event_body parameter is the SQL statement that the event will execute.

The schedule parameter can be a specific date and time, or it can be a recurring schedule. For example, the following statement creates a one-time event that will be executed at 10:00 AM on January 1st, 2023:

SQL

CREATE EVENT my_event
ON SCHEDULE AT '2023-01-01 10:00:00'
DO UPDATE my_table SET my_column = 1;

The following statement creates a recurring event that will be executed every day at 10:00 AM:

SQL

CREATE EVENT my_event
ON SCHEDULE EVERY DAY AT 10:00:00
DO UPDATE my_table SET my_column = 1;

The event_body parameter can be any valid SQL statement. For example, the following statement creates an event that will update the my_column column in the my_table table to 1:

SQL

CREATE EVENT my_event
ON SCHEDULE AT '2023-01-01 10:00:00'
DO UPDATE my_table SET my_column = 1;