This website is for reference purposes only. Users are responsible for any misuse. The owner is not liable for any consequences.
Back to Database Management Systems (Laboratory)
8.1.3HardCODE

Trigger after UPDATE

Question

Solution

SQL

CREATE OR REPLACE FUNCTION update_product_quantity()
RETURNS TRIGGER AS $$
BEGIN
	UPDATE products
	SET quantity = quantity + OLD.quantitysold - NEW.quantitysold
	WHERE productid = NEW.productid;

	RETURN NULL;
END;

$$ LANGUAGE plpgsql;

CREATE TRIGGER after_sale_update
AFTER UPDATE ON sales
FOR EACH ROW
EXECUTE FUNCTION update_product_quantity()

1/1 test cases passed

2/2 hidden test cases passed