Events in MySQL are tasks that run according to a defined Schedule. events are executed based on schedule so it is also called a Scheduled event. in this article, we will be learning about events with MySQL.
Events can be created for one-time execution or for certain intervals. It consists of some MySQL statements which get executed on the happening of the event.
Application of Event:
- The event can be used for various optimization over collected data on a frequent basis like weekly, or monthly.
- It can be used to track various details on available data.
- This is used to maintain a large eCommerce-based database where there is a need of monitoring the stock units of available products.
- It can be used to clean up log records of the visiting users on-site weekly or after a certain time,
Scheduled events are executed by a special thread called an event scheduler. we can check all running events' names by typing Command.
SHOW EVENTS;
The above statement will list all the events with their name. In order to execute any event firstly we need to ensure that the event scheduler is set to ON
Event Scheduler:
To check the status of the event scheduler we can execute the following Command,
SHOW VARIABLES
WHERE VARIABLE_NAME =
'event_scheduler';
Output:
if the status of the event scheduler_id Off then we can set it as ON by executing Following Command.
SET GLOBAL event_scheduler = ON:
Creating an event:
Syntax:
CREATE EVENT event_name
ON SCHEDULE schedule
DO
event_body
The event name must be unique. To execute the event in a repetitive manner EVERY keyword can be used.
let's create a simple event that will check after every 10 minutes how many stocks are there in the table. We will use the following schema for Product_mastr:
product_id | product_name | Qty |
---|
001 | headphones | 20 |
002 | Mobile Phone | 10 |
We will have another empty table we named as order_stock. This table will have the data which need to be ordered by shop keeper these things never get out of stock.
create an event that checks after every 10 minutes on the Product_mastr table, to track the product quantity and insert quantity with a timestamp.
mysql> DELIMITER //
mysql> CREATE EVENT LOG_QTY
-> ON SCHEDULE EVERY 10 MINUTE
-> DO BEGIN
-> insert into order_stock(product_id,qty_which_needToBeOrdered,time)
select product_id,qty as qty_which_needToBeOrde
red,CURRENT_TIMESTAMP() from product_mastr;
-> END;
-> //
Now lets Check the ORDER_STOCK table:
select * from order_stock;
Output:
log record would be added in a repetitive manner to the order_stock table after every 10 minutes. So this is How events can be useful for various purposes Like tracking of various data, Table optimization or clearing this kind of records after certain time, all this kind off things can be achieved with the help of MySQL Event.
Similar Reads
MySQL ADDTIME() function MySQL ADDTIME() function adds the specified time intervals to the given date and time. It returns the date or DateTime value after adding the time interval. SyntaxThe MySQL ADDTIME() function syntax is: ADDTIME(expr1, expr2) ParameterADDTIME() function accepts two parameters. expr1: The given dateti
2 min read
How To Disable the Running Event in MySQL? Pre-requisites: create an event in MySQL, Events in MySQL Events in MySQL are tasks that run according to a defined Schedule. events are executed based on schedule so it is also called a Scheduled event. In this article, we will talk about disabling running events. Steps for Disable the Running Even
1 min read
What is MySQL? MySQL is an open-source, relational database management system (RDBMS) that uses Structured Query Language (SQL) to manage and manipulate data. It is one of the most popular database systems used in web applications, known for its speed, reliability, and ease of use. MySQL is commonly used in conjun
5 min read
What is MySQL? MySQL is an open-source, relational database management system (RDBMS) that uses Structured Query Language (SQL) to manage and manipulate data. It is one of the most popular database systems used in web applications, known for its speed, reliability, and ease of use. MySQL is commonly used in conjun
5 min read
MySQL Date and Time Functions Handling date and time data in MySQL is essential for many database operations, especially when it comes to handling timestamps, scheduling tasks, or generating time-based. MySQL provides a variety of date and time functions that help users work with date values, perform calculations, and format the
6 min read
MySQL | LEAD() and LAG() Function The LEAD() and LAG() functions in MySQL are powerful window functions introduced in version 8.0. They allow users to access data from preceding or following rows without the need for self-joins or subqueries, making them invaluable for analyzing sequential or time-series data.In this article, we wil
3 min read