
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
MySQL Trigger to Execute Multiple Statements
In MySQL, triggers are a mechanism that allow for specific actions to be executed automatically when specific events like Insert, Update, or Delete occur in a table. They are useful as they automate processes and maintain data integrity.
Using BEGIN...END in trigger
In a trigger, if we want to perform more than one action we write it within the BEGIN?END block. This block simplifies the way we can manage a larger number of actions for a more complicated task.
Let us see how the BEGIN...END construct allows multiple statements to be executed within a trigger.
We can group multiple statements within the same trigger with the help of Begin...End in MySQL. This allows us to perform a series of action and modifications when a trigger is activated. Within the BEGIN block, we can also use another syntax that is permitted within stored routines such as conditionals(IF, ELSEIF) and loops(WHILE, REPEAT).
For illustrating the concept, we are using the following example of BEFORE INSERT TRIGGER having the IF conditional statements.
Example
This example creates a BEFORE INSERT trigger on student_age table. Whenever the row is added to the table, the trigger gets the value in the age column and keeps it within the reasonable range 0 to 100. By setting it to 0 if less than 0 and to 100 if greater than 100.
Creating tableLet us create a table called Student_age which stores the information about student's name and age. Following is the query -
Creating a triggerCREATE TABLE Student_age ( name VARCHAR(50), age INT );
The following code creates a trigger that validates and adjusts the age value before inserting it into the student_age table.
DELIMITER // CREATE TRIGGER before_insert_studentage BEFORE INSERT ON student_age FOR EACH ROW BEGIN IF NEW.age < 0 THEN SET NEW.age = 0; ELSEIF NEW.age > 100 THEN SET NEW.age = 100; END IF; END // DELIMITER ;
Let's break down the components of this query and understand how it works. Below is an explanation of the key elements used in creating the trigger.
DELIMITER //: Changes the default delimiter(;) in order to allow creating a multi-statement trigger.
BEGIN...END Block: The BEGIN...END block executes multiple statements in the trigger.
BEFORE INSERT ON: The Before Insert Trigger is executed right before a value is being inserted into a database table.
Conditionals (IF, ELSEIF): The IF statement checks if NEW.age is below 0 or above 100, adjusting the value accordingly.
Now, let us test the trigger by inserting values into student_age table. To insert records into a table we use INSERT command.
VerificationINSERT INTO student_age (name, age) VALUES ('harry', -9);
Let us check whether the trigger got activated, for this we need to fetch the data using SELECT command.
SELECT * FROM student_age;
Following is the output of the above query ?
name | age |
---|---|
harry | 0 |
To ensure that only valid data is present in the student_age table, the trigger has set the age to 0 based on the condition given.
Benefits of using BEGIN...END in trigger:
Error prevention: You can prevent errors ensuring whether the correct data is inserted or updated checking data integrity.
Supports Loops: BEGIN...END allows loops (WHILE,REPEAT) for complex tasks. If you need to check numerous values or repeat an action for multiple criteria, this can be helpful.
Conditional logic: you can also use IF, ELSE, and CASE statements within the BEGIN...END block. As a result, conditions can be checked and various actions can be taken upon on the data, such as changing numbers that fall outside of specific ranges.