MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
In MySQL, an event is a task that runs based on a schedule. Therefore, it's also known as a scheduled event. Events are similar to cron jobs in UNIX or tasks in Task Scheduler in Windows.
The event scheduler is a MySQL feature that allows you to schedule tasks that run within the database environment. The tasks, also known as events, are executed according to a specific schedule.
Let's see how we can create an event in MySQL:
Enable Event Scheduler
First, make sure that event scheduler is enabled. You can check its status using the following command:
SHOW VARIABLES LIKE 'event_scheduler';
If the event scheduler is disabled, you can turn it on by running this command:
SET GLOBAL event_scheduler = ON;
Syntax to CREATE EVENT
Here is the basic syntax for creating an event:
CREATE EVENT event_name ON SCHEDULE schedule DO event_body;
Where:
event_name
: The name of the event.schedule
: Defines when and how often an event is executed.event_body
: The SQL statement to be executed.The schedule
can be a single occurrence or a recurring event. For example, AT timestamp [+ INTERVAL]
or EVERY interval STARTS timestamp [+ INTERVAL] ENDS timestamp [+ INTERVAL]
.
Example
Let's consider an example where we create an event that deletes all rows from a log_table
every day at 3 AM.
CREATE EVENT clear_log_table ON SCHEDULE EVERY 1 DAY STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 3 HOUR) DO DELETE FROM log_table;
In this example, clear_log_table
is an event that is scheduled to run every day (EVERY 1 DAY
), starting at 3 AM (STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 3 HOUR)
). The action of this event is to delete all rows from log_table
.
Note:
EVENT
privilege for the database to create an event.SHOW EVENTS
statement.DROP EVENT event_name
statement.ALTER EVENT event_name
statement.Creating scheduled events in MySQL:
-- Create a scheduled event in MySQL CREATE EVENT event_name ON SCHEDULE EVERY 1 DAY DO BEGIN -- Event logic END;
How to use CREATE EVENT in MySQL:
CREATE EVENT
statement in MySQL to define and create a new scheduled event.-- Create a scheduled event in MySQL CREATE EVENT event_name ON SCHEDULE EVERY 1 HOUR DO BEGIN -- Event logic END;
MySQL event scheduling example:
-- Create a daily event to update timestamp CREATE EVENT update_timestamp_event ON SCHEDULE EVERY 1 DAY DO UPDATE my_table SET timestamp_column = NOW();
Defining events with MySQL:
CREATE EVENT
statement, specifying the event name, schedule, and the event logic.-- Create a scheduled event in MySQL CREATE EVENT event_name ON SCHEDULE EVERY 1 WEEK DO BEGIN -- Event logic END;
Creating and managing scheduled events in MySQL:
CREATE EVENT
statement and altering or dropping existing events.-- Create a scheduled event in MySQL CREATE EVENT event_name ON SCHEDULE EVERY 30 MINUTE DO BEGIN -- Event logic END; -- Alter a scheduled event in MySQL ALTER EVENT event_name ON SCHEDULE EVERY 1 HOUR;
Using parameters in MySQL events:
-- Create an event with parameters in MySQL CREATE EVENT event_name ON SCHEDULE EVERY 2 DAYS DO BEGIN -- Event logic using parameters END;
Executing and testing MySQL events:
-- Manually execute the event CALL event_name;