始める無料で始める

Creating a trigger to keep track of data changes

The Fresh Fruit Delivery company needs to keep track of any new items added to the Products table. You can do this by using a trigger.

The new trigger will store the name, price, and first introduced date for new items into a ProductsHistory table.

この演習はコースの一部です

Building and Optimizing Triggers in SQL Server

コースを見る

演習の手順

  • Create the ProductsNewItems trigger on the Products table.
  • Set the trigger to fire when data is inserted into the table.

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

-- Create a new trigger
CREATE TRIGGER ProductsNewItems
ON ___
AFTER ___
AS
	-- Add details to the history table
	INSERT INTO ProductsHistory(Product, Price, Currency, FirstAdded)
	SELECT Product, Price, Currency, GETDATE()
	FROM inserted;
コードを編集して実行