Introduction to MySQL UPDATE Trigger
MySQL UPDATE_TRIGGER is one of the triggers that enable the update actions on the specified table. On a general note, a trigger can be defined as a set of instructions or steps which perform the intended change action automatically, on the specified table. The possible change actions can be INSERT, UPDATE, or DELETE. The concept of triggers is similar to stored procedures and the execution helps in saving time and effort in writing the queries. A trigger is an explicitly defined database object associated with a table and is defined using the ‘CREATE TRIGGER’ statement. We will discuss details of both CREATE TRIGGER and UPDATE TRIGGER statements in detail.
Syntax
Just like any other set of statements, TRIGGERS are also be created and dropped as per requirements. The CREATE trigger syntax is as below:
CREATE
[DEFINER = user]
TRIGGER trigger_name
trigger_time trigger_event
ON tbl_name FOR EACH ROW
[trigger_order]
trigger_body
Explanation:
DEFINER: definer is the user who is entitled with the privilege to perfor the trigger operations.
trigger_name: trigger_name is the name of trigger instruction. This is a user-defined field.
trigger_time: trigger_time is the moment in which trigger is to be initiated. It is either BEFORE or AFTER indicating whether to perfor the trigger action before each row or after.
trigger_event: trigger_event is the change operation referred upon. INSERT, UPDATE or DELETE actions are used as triggers.
trigger_order: trigger order specifies if the trigger FOLLOWS or PRECEDES
trigger_body: trigger_body defines the trigger actions. If more than one statement is to be mentioned, it if preferred to use the BEGIN END block and also change the default delimiters.
So the syntax can be shown as:
DELIMITER $$
CREATE TRIGGER trigger_name
trigger_time trigger_event
ON table_name FOR EACH ROW
BEGIN
-- statements
END $$
DELIMITER;
How does UPDATE Trigger function works in MySQL
From the above discussion, we got to know there can be two UPDATE triggers, either BEFORE UPDATE or AFTER UPDATE. The difference in the working of these triggers is that the action is performed on each row before updating or after updating.
AFTER UPDATE trigger
Let us consider two tables to understand the UPDATE trigger in detail. The first table is InitialSales and the second table is SalesUpdates. Below is gives an idea of columns in the table.
Initial Sales table:
sales update table is defined as:
Code:
CREATE TABLE SalesUpdates (
prodId INT AUTO_INCREMENT PRIMARY KEY,
sales_Id INT,
InitialQuantitiy INT,
UpdatedQuantity INT,
UpdatedOn TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
Let’s define the Trigger as below:
Code:
DELIMITER $$
CREATE TRIGGER Updated_Sales_Data
AFTER UPDATE
ON InitialSales FOR EACH ROW
BEGIN
IF OLD.qty<>new.qty THEN
INSERT INTO SalesUpdates(sales_Id,InitialQuantity, UpdatedQuantity)
VALUES(old.prodId, old.qty, new.qty);
END IF;
END $$
DELIMITER;
Output:
Explanation: The trigger name is Updated_Sales_Data. The trigger_event is “UPDATE” and trigger_time is “AFTER”. The trigger will act upon the table “InitialSales”. As per query, if any update is made to column “qty” in InitialSales table, a new entry will be added in the SalesUpdatetable, with old value and the new value of the “qty” column. Any updates made to the InitialSales table will automatically initiate the trigger. No need to explicitly execute the trigger every time.
To further check on the working of TRIGGER, let’s do two types of updates to the InitialSales table.
First, we will see updates made to the “qty” column of only one ‘prodId’.
Consider InitialSales table as below:
Code:
UPDATE InitialSales
SET qty = 500
WHERE prodId = 3;
Query will update the column ‘qty’ of prodId ‘3’, from ‘150’ to ‘500’.
Output:
Let’s see both tables InitialSales and SalesUpdates:
InitialSales:
SalesUpdates:
If we look into this scenario, we can understand the below things:
- Update made to qty column in InitialSales table reflects properly
- SalesUpdates table has data only for the updated row of InitialSales table, which is a row of prodId = 3.
- The UPDATE TRIGGER was not explicitly called, but it automatically updated theSalesUpdate table.
- The SalesUpdates table holds old and new values of qty column
In the second scenario, let’s make an update to all three rows of the InitialSales table.
InitialSales:
Code:
UPDATE initialSales
SET qty = CAST(qty * 1.5 AS UNSIGNED);
The query updates an increment to ‘qty’ column of all three rows in InitialSales table by 50%
Output:
InitialSales:
SalesUpdates:
The first row of the SalesUpdate table is the first update made to the InitialSales table ( by our first query). So, we can see that the SalesUpdate table holds the history of all updates made, along with a timestamp.
BEFORE UPDATE trigger
Thus we have clearly understood the AFTER UPDATE trigger. Let’s try and familiarise ourselves with the BEFORE UPDATE trigger now.
For the BEFORE UPDATE trigger, let us consider the tables as below:
InitialSales:
SalesUpdates:
Code:
CREATE TABLE SalesUpdates (
sales_Id INT AUTO_INCREMENT PRIMARY KEY,
prodId INT,
InitialQuantitiy INT,
UpdatedQuantity INT,
Updated On TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
Let’s define the Trigger as below:
Code:
DELIMITER $$
CREATE TRIGGER Updated_Sales_Data
BEFORE UPDATE
ON InitialSales FOR EACH ROW
BEGIN
IF OLD.qty<>new.qtyTHEN
INSERT INTO SalesUpdates(sales_Id,InitialQuantity, UpdatedQuantity)
VALUES(old.prodId, old.qty, new.qty);
END IF;
END $$
DELIMITER;
Output:
Explanation: The trigger name is Updated_Sales_Data. The trigger_event is “UPDATE” and trigger_time is “BEFORE”. The trigger will act upon the table “InitialSales”. As per query, if any update is made to column “qty”in InitialSales table, a new entry will be added in the SalesUpdatetable, with old value and new value of “qty” column. Any updates made to the InitialSales table will automatically initiate the trigger. No need to explicitly execute the trigger every time.
We have our InitialSales table as below:
And SalesUpdate table as below:
Let’s execute a query to make an update in the qty column in the InitialSales table.
Code:
UPDATE InitialSales
SET qty = 500
WHERE prodId = 3;
Output:
InitialSales table: the qty value in this table is updated to ‘1000’ for prodId = 1 from value ‘600’.
SalesUpdates table: the initial value and updated value of qty column are displayed, along with the update_timestamp.
In general, we can say, the BEFORE UPDATE trigger is used to make an update before it is committed to the database and AFTER UPDATE trigger updates after the database commit.
Conclusion
In this section, we discussed the UPDATE triggers. A trigger can either be a BEFORE trigger or an AFTER trigger. In the CREATE trigger statement, we define the trigger_event as UPDATE for UPDATE trigger, trigger_time as BEFORE or AFTER as per requirement, trigger name, trigger body, and the table on which trigger is to be performed. And for the UPDATE trigger, once the highlighted column faces any updates, the trigger is automatically called and the update table is updated.
Recommended Articles
This is a guide to MySQL UPDATE Trigger. Here we discuss an introduction to MySQL UPDATE Trigger, syntax and how does it work with query examples. You can also go through our other related articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses