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
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
5 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15+ min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Balance Factor = left subtree height - right subtree heightFor a Balanced Tree(for every node): -1 ⤠Balance Factor ⤠1Example of an
4 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
What is a Neural Network? Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental
12 min read