Triggers vs. computed columns
While continuing your analysis of the database, you find two other interesting objects:
The table
SalesWithPricehas a column that calculates theTotalAmountasQuantity * Price. This is done using a computed column which uses columns from the same table for the calculation.The trigger
SalesCalculateTotalAmountwas created on theSalesWithoutPricetable. ThePricecolumn is not part of theSalesWithoutPricetable, so a computed column cannot be used for theTotalAmount. The trigger overcomes this limitation by using thePricecolumn from theProductstable.
この演習はコースの一部です
Building and Optimizing Triggers in SQL Server
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
-- Add the following rows to the table
INSERT INTO ___ (Customer, Product, Price, Currency, Quantity)
VALUES ('Fruit Mag', 'Pomelo', 1.12, 'USD', 200),
('VitaFruit', 'Avocado', 2.67, 'USD', 400),
('Tasty Fruits', 'Blackcurrant', 2.32, 'USD', 1100),
('Health Mag', 'Kiwi', 1.42, 'USD', 100),
('eShop', 'Plum', 1.1, 'USD', 500);
-- Verify the results after adding the new rows
SELECT * FROM SalesWithPrice;