
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is MySQL Event and How it Relates to Trigger
Events perform tasks based on a predefined schedule, such as cleaning data once a day or doing backups, while Triggers are activated automatically when some actions are executed, like an INSERT, UPDATE, or DELETE.
Despite their differences, events, and triggers can be used together to manage scheduled and reactive automation tasks. This article will show you the relationship and illustrate how they can enhance database management when combined effectively.
Relationship Between MySQL Events and Triggers
By making use of the strengths of events and triggers, we can design a ideal workflow that can efficiently handle real-time responses and scheduled maintenance.
- While events and triggers function independently, we can use them together in a scenario where:
- Triggers mark or set up the record for deletion when they are no longer active.
- The events that run every night will remove those flagged records.
Syntax
Following is the syntax to create a MYSQL event ?
CREATE EVENT event_name ON SCHEDULE schedule DO Event_body
Syntax
Following is the syntax to create a MYSQL trigger ?
CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EACH ROW BEGIN -- SQL statements END;
Example
In this example we will use a trigger to flag employee as "inactive" if they haven't logged in for 30 days. The trigger will update the status field and set a deleted_at timestamp to mark them for deletion and we will use a scheduled event to delete those flagged employees every night.
In the following query, we are creating a table named "Employee" using the CREATE TABLE Statement ?
CREATE TABLE Employee ( ID INT AUTO_INCREMENT PRIMARY KEY, emp_name VARCHAR(100), last_login DATETIME, status VARCHAR(20) DEFAULT 'active', deleted_at DATETIME NULL );
Now, lets populate the Employee table using the INSERT query.
INSERT INTO Employee (emp_name, last_login) VALUES ('John', '2024-09-01'), ('Harry', '2024-10-15'), ('Alice', '2024-10-01'), ('Bob', '2024-10-11');
The contents of the Employee table will be as follows ?
ID | emp_name | last_login | status | deleted_at |
---|---|---|---|---|
1 | John | 2024-09-01 | active | NULL |
2 | Harry | 2024-10-15 | active | NULL |
3 | Alice | 2024-10-01 | active | NULL |
4 | Bob | 2024-10-11 | active | NULL |
Triggers to flag employee for deletion
Let us create a trigger that sets the status to "inactive" and records the deleted_at timestamp for employee who haven't logged in for 30 days.
DELIMITER # CREATE TRIGGER flag_inactive_employee BEFORE UPDATE ON Employee FOR EACH ROW BEGIN IF TIMESTAMPDIFF(DAY, NEW.last_login, NOW()) > 30 THEN SET NEW.status = 'inactive'; SET NEW.deleted_at = NOW(); END IF; END # DELIMITER ;
Event to Delete Inactive Employees
The following query runs daily to remove flagged employee for deletion.
DELIMITER # CREATE EVENT delete_inactive_users ON SCHEDULE EVERY 1 DAY STARTS '2024-11-19 00:00:00' DO BEGIN DELETE FROM Employee WHERE deleted_at IS NOT NULL; END # DELIMITER ;
Verification
Let us update the last_login value for John to simulate a scenario where the trigger would be executed.
To modify existing records in a table we use UPDATE query.
UPDATE Employee SET last_login = '2024-07-01' WHERE emp_name = 'John';
After executing the above query, trigger will be activated and modifies the changes to the table. Let us fetch the modified record to verify the change using SELECT command
SELECT * FROM Employee WHERE username = 'John';
Following is the output of the above code -
ID | emp_name | last_login | status | deleted_at |
---|---|---|---|---|
1 | John | 2024-07-01 | inactive | 2024-11-20 11:03:57 |
After the scheduled event delete_inactive_users runs (e.g., at midnight on 2024-11-20), it will delete all rows where deleted_at is not NULL.
Now, running the following query -
SELECT * FROM Employee;
The contents of the Employee table will be as follows ?
ID | emp_name | last_login | status | deleted_at |
---|---|---|---|---|
2 | Harry | 2024-11-15 | Active | NULL |
3 | Alice | 2024-10-01 | active | NULL |
4 | Bob | 2024-10-11 | active | NULL |
This will be expected output when the event runs at midnight, the flagged record John was deleted from the database as he was inactive.
Conclusion
MySQL Events and Triggers make each other effective when used for automation. Events ensure that there is a periodic execution of specified tasks whereas, triggers respond to changes made to the database which makes them appropriate for helping the efficiency and the integrity of databases.