Or copy link
Copy link
Automating tasks in MySQL, like backups, data cleaning, and updates, makes your work easier by removing the need for manual effort. A MySQL event is a task that runs at a specific time or on a set schedule. Each event can have one or more MySQL statements, which are saved in the database and executed when scheduled.
In this article, we will provide simple, step-by-step instructions on how to use the CREATE EVENT statement to create and manage these tasks effectively.
Events are like stored procedures, but they run automatically on a set/specified schedule. In MySQL database, the CREATE EVENT statement is used to create a new event.
The basic syntax to create an event in MySQL is as follows:
CREATE EVENT eventName ON SCHEDULE dateTime DO eventBody;
In this syntax:
Let’s learn how to create and schedule events in MySQL using examples.
Build Blazing-fast Websites with MySQL Hosting!
Ultahost’s high-performance pre-configured servers ensure fast MySQL websites and apps with easy setup, scalability, and expert support!
Let’s create a new table named employee_info using the CREATE TABLE statement as shown below:
CREATE TABLE employee_info (FullName VARCHAR(255), Age INT);
The table contains two columns FullName and Age:
Now run the CREATE EVENT statement to create an event named add_employee_event. This event is set to insert a record into the employee_info table one minute after execution:
CREATE EVENT add_employee_event ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE DO INSERT INTO employee_info VALUES('Anees Asghar', 28);
After one minute, you can verify the contents of the employee_info table using the SELECT statement as follows:
The output confirms that a new record has been successfully inserted into the employee_info table at the scheduled time.
You can use the IF NOT EXISTS clause with the CREATE EVENT statement to create a new event if it doesn’t already exist. If an event with the same name already exists, the query will just be ignored.
For example, in the following query, we tried to create an event with the same name that already exists, as a result, we face the following error:
To avoid this error, we run the CREATE EVENT statement with the IF NOT EXISTS clause as follows:
CREATE EVENT IF NOT EXISTS add_employee_event ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE DO INSERT INTO employee_info VALUES('Anees Asghar', 28);
This time, the query executed successfully without showing any error:
The 1 warning in the output indicates that the event with the name add_employee_event already existed and was not recreated, but the query itself did not fail.
Read also How To Use MySQL / MariaDB From Command Line
We can run the CREATE EVENT statement with a COMMENT clause to add a descriptive note to an event. For example, the following query schedules an event and includes a comment for documentation purposes:
CREATE EVENT IF NOT EXISTS event_truncate_after_2_days ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 2 DAY COMMENT 'This truncates the employee_info table after 2 days' DO TRUNCATE TABLE employee_info;
This way, we can add a comment to indicate the purpose of the event, which, in this case, is to truncate the employee_info after 2 days.
We can use the CREATE EVENT statement with the EVERY clause to schedule events to run at regular intervals. For example, the following query truncates the employee_info table every 2 days:
CREATE EVENT IF NOT EXISTS event_truncate_2_days ON SCHEDULE EVERY 2 DAY COMMENT 'This truncates the employee_info table every 2 days' DO TRUNCATE TABLE employee_info;
This event will execute the TRUNCATE TABLE command for employee_info every 2 days.
That’s all about creating and scheduling events in MySQL.
Events in MySQL help us make tasks easier by automating important jobs like backups, data cleaning, and updates. With the CREATE EVENT statement, you can set up tasks to run automatically at specific times or on a regular schedule. Moreover, we can use the CREATE EVENT statement with different clauses to handle common scenarios, such as avoiding errors, adding useful notes, scheduling tasks at regular intervals, etc. In this article, we covered different examples of creating and scheduling events in MySQL.
We hope this guide has empowered you to create and schedule events in MySQL effectively. Consider using Ultahost dedicated server hosting, which provides the control and security needed for a smooth database experience. With root access to your server, you can easily install and configure MySQL and set up events to automate your database tasks efficiently.
A MySQL event is a scheduled task that runs automatically at a specific time or on a defined schedule. It can execute one or more SQL statements as specified in its definition.
You can create a MySQL event using the CREATE EVENT statement, specifying the event name, schedule, and the SQL statements to be executed.
You can add comments using the COMMENT clause in the CREATE EVENT statement. This helps document the purpose of the event for future reference.
Yes, you can use the EVERY clause to schedule an event to run at regular intervals, such as every day or every few days.
You can verify the execution of a scheduled event by querying the relevant tables after the scheduled time to see if the expected changes were made.
Yes, you can modify an existing event using the ALTER EVENT statement, allowing you to change its schedule or SQL statements.
No, while both can execute SQL statements, events run automatically on a schedule, whereas stored procedures must be called manually.
PostgreSQL, also known as Postgres is a powerful open-s...
MySQL Workbench is a popular graphical tool that simpli...
phpPgAdmin is a free, open-source web-based tool that s...
Making a copy of your MySQL database, also known as a b...
SQL Server is a relational database management system (...
The "Error Establishing a Database Connection" message ...
Save my name, email, and website in this browser for the next time I comment.
Δ