Zde je návod, jak to udělat v MySQL (vaše otázka je označena jak mysql
a sql-server
)
CREATE TRIGGER tg_bi_order_contains_items
BEFORE INSERT ON Order_contains_items
FOR EACH ROW
SET NEW.Ordered_price =
(
SELECT Unit_price * NEW.Quantity_ordered
FROM Item
WHERE Item_id = NEW.Item_id
LIMIT 1
);
CREATE TRIGGER tg_bu_order_contains_items
BEFORE UPDATE ON Order_contains_items
FOR EACH ROW
SET NEW.Ordered_price =
(
SELECT Unit_price * NEW.Quantity_ordered
FROM Item
WHERE Item_id = NEW.Item_id
LIMIT 1
);
Zde je SQLFiddle demo