mysql trigger after update 1

mysql trigger after update

CREATE TRIGGER after_sales_update
AFTER UPDATE
ON sales FOR EACH ROW
BEGIN
    IF OLD.quantity <> new.quantity THEN
        INSERT INTO SalesChanges(salesId,beforeQuantity, afterQuantity)
        VALUES(old.id, old.quantity, new.quantity);
    END IF;
END$$

Here is what the above code is Doing:
1. Create a table called SalesChanges.
2. Create a trigger called after_sales_update.
3. The trigger will fire after an update on the sales table.
4. The trigger will fire for each row that is updated.
5. If the quantity in the sales table is different from the quantity in the sales table before the update, then insert a row into the SalesChanges table.

The SalesChanges table will have three columns:
1. salesId – the id of the sales row that was updated.
2. beforeQuantity – the quantity of the sales row before the update.
3. afterQuantity – the quantity of the sales row after the update.

The SalesChanges table will be used to track changes to the quantity of sales.

The SalesChanges table will be used to track changes to the quantity of sales.

Similar Posts