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.1HardCODE

Trigger after INSERT

Question

Solution

SQL

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

	return NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER after_sale_insert
AFTER INSERT ON sales
FOR EACH ROW
EXECUTE FUNCTION update_product_quantity()

1/1 test cases passed

2/2 hidden test cases passed