Skip to content

NEW

CREATE TRIGGER update_product_quantity
BEFORE UPDATE ON products
FOR EACH ROW
BEGIN
IF NEW.quantity < 0 THEN
SET NEW.quantity = 0;
END IF;
END;

Trigger update_product_quantity is created with no return message.

In the MySQL trigger, the NEW keyword is used to access the column of the new row being inserted or updated. The NEW.quantity represents the value of the quantity column of the new row that is being inserted or updated. In this example, a trigger is defined to update the quantity of products. If the new quantity value is less than 0, it is set to 0. This ensures there’s no negative value in the quantity column after an update.