Get Started

Practicing creating triggers

The Fresh Fruit Delivery company needs help creating a new trigger called OrdersUpdatedRows on the Orders table.

This trigger will be responsible for filling in a historical table (OrdersUpdate) where information about the updated rows is kept.

A historical table is often used in practice to store information that has been altered in the original table. In this example, changes to orders will be saved into OrdersUpdate to be used by the company for auditing purposes.

This is a part of the course

“Building and Optimizing Triggers in SQL Server”

View Course

Exercise instructions

  • Create the new trigger for the Orders table.
  • Set the trigger to be fired only after UPDATE statements.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

-- Set up a new trigger
___ TRIGGER OrdersUpdatedRows
ON ___
-- The trigger should fire after UPDATE statements
___ UPDATE
-- Add the AS keyword before the trigger body
AS
	-- Insert details about the changes to a dedicated table
	___ INTO OrdersUpdate(OrderID, OrderDate, ModifyDate)
	SELECT OrderID, OrderDate, GETDATE()
	FROM inserted;
Edit and Run Code

This exercise is part of the course

Building and Optimizing Triggers in SQL Server

IntermediateSkill Level
5.0+
4 reviews

Learn how to design and implement triggers in SQL Server using real-world examples.

An introduction to the basic concepts of SQL Server triggers. Create your first trigger using T-SQL code. Learn how triggers are used and what alternatives exist.

Exercise 1: IntroductionExercise 2: Types of triggerExercise 3: Creating your first triggerExercise 4: Practicing creating triggers
Exercise 5: How DML triggers are usedExercise 6: When to use triggersExercise 7: Creating a trigger to keep track of data changesExercise 8: Trigger alternativesExercise 9: Triggers vs. stored proceduresExercise 10: Triggers vs. computed columns

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free