Inilah cara Anda melakukannya di MySQL (pertanyaan Anda ditandai dengan mysql
dan 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
);
Ini SQLFiddle demo