If you're working with a MySQL database, you may need to update a table based on the data in another table. One way to do this is by using a trigger, which is a stored procedure that automatically executes in response to certain events. In this guide, we'll walk you through the process of creating a trigger to update a MySQL table.
What is a Trigger?
A trigger is a special kind of stored procedure that is automatically executed by the database server in response to certain events. These events can include inserting, updating, or deleting data in a table. Triggers are useful for enforcing data integrity, implementing business rules, and auditing changes to the database.
Creating a Trigger in MySQL
To create a trigger in MySQL, you can use the CREATE TRIGGER statement. The basic syntax is as follows:
CREATE TRIGGER trigger_name
AFTER | BEFORE
{INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
trigger_body;
Here's a breakdown of the syntax:
trigger\_name: The name of the trigger.AFTERorBEFORE: The timing of the trigger. AnAFTERtrigger is executed after the event, while aBEFOREtrigger is executed before the event.INSERT,UPDATE, orDELETE: The event that will trigger the stored procedure.table\_name: The name of the table that the trigger is associated with.FOR EACH ROW: Indicates that the trigger should be executed for each row affected by the event.trigger\_body: The stored procedure that will be executed when the trigger is activated.
Updating a Table with a Trigger
Let's say you have two tables: orders and order\_items. The orders table has the following columns:
id: The primary key of the table.customer\_id: The ID of the customer who placed the order.status: The status of the order.
The order\_items table has the following columns:
id: The primary key of the table.order\_id: The ID of the order that the item belongs to.product\_id: The ID of the product that was ordered.quantity: The quantity of the product that was ordered.price: The price of the product at the time of the order.
Now, let's say you want to update the status column in the orders table to 'completed' when all of the items in the order\_items table for that order have been shipped.
To do this, you can create a trigger on the order\_items table that checks the status of all the items in the table for a given order. If all of the items have been shipped, the trigger can update the status column in the orders table to 'completed'.
CREATE TRIGGER update_order_status
AFTER UPDATE
ON order_items
FOR EACH ROW
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE num\_shipped INT;
DECLARE cur CURSOR FOR
SELECT COUNT(*)
FROM order_items
WHERE order_id = NEW.order\_id
AND shipped = TRUE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
SELECT COUNT(\*)
INTO num\_shipped
FROM order\_items
WHERE order\_id = NEW.order\_id
AND shipped = TRUE;
IF num\_shipped = (SELECT COUNT(\*) FROM order\_items WHERE order\_id = NEW.order\_id) THEN
UPDATE orders
SET status = 'completed'
WHERE id = NEW.order\_id;
END IF;
CLOSE cur;
END;
Let's break down the trigger:
DECLARE done INT DEFAULT FALSE;: Declare a variable to track whether the cursor has reached the end of the result set.DECLARE num\_shipped INT;: Declare a variable to track the number of items that have been shipped.DECLARE cur CURSOR FOR SELECT COUNT(\*) FROM order\_items WHERE order\_id = NEW.order\_id AND shipped = TRUE;: Declare a cursor to count the number of items that have been shipped for the given order.DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;: Declare a continue handler to set thedonevariable to true when the cursor reaches the end of the result set.OPEN cur;: Open the cursor.SELECT COUNT(\*) INTO num\_shipped FROM order\_items WHERE order\_id = NEW.order\_id AND shipped = TRUE;: Count the number of items that have been shipped for the given order.IF num\_shipped = (SELECT COUNT(\*) FROM order\_items WHERE order\_id = NEW.order\_id) THEN: Check if all of the items in the order have been shipped.UPDATE orders SET status = 'completed' WHERE id = NEW.order\_id;: If all of the items in the order have been shipped, update the status of the order.CLOSE cur;: Close the cursor.
Now, whenever an item in the order\_items table is updated to indicate that it has been shipped, the trigger will check the status of all the items in the table for that order. If all of the items have been shipped, the trigger will update the status column in the orders table to 'completed'.
Triggers are a powerful tool for automating the maintenance of your MySQL database. In this guide, we've shown you how to create a trigger to update a table based on the data in another table. You can use this technique to enforce data integrity, implement business rules, and audit changes to the database. With a little practice, you'll be able to create triggers that automate the maintenance of your database and save you time and effort.
References
| Title | URL |
|---|---|
| MySQL Trigger Syntax | https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html |
| MySQL CREATE TRIGGER Statement | https://www.w3schools.com/sql/sql_trigger_syntax.asp |
| MySQL Triggers Tutorial | https://www.mysqltutorial.org/mysql-triggers.aspx |