SQL-Server Triggers  «Prev  Next»

Lesson 5 Creating triggers, part 1
Objective Understand how to create a trigger.

Creating Triggers in SQL-Server(

Creating triggers with Enterprise Manager

You can create a trigger with either Enterprise Manager or Transact-SQL.
To use the Enterprise Manager to create triggers:
  1. Right-click the name of the table that the trigger will be assigned to.
  2. Choose the All Tasks | Manage Triggers menu option.
  3. Enter the appropriate Transact-SQL statements in the Trigger Properties dialog box, shown below:
Creating a new trigger

Creating Triggers

A trigger can be fi red for any combination of insert, update, or delete events. Triggers are created and modifi ed with the standard DDL commands, CREATE, ALTER, and DROP, as follows:
CREATE TRIGGER Schema.TriggerName ON Schema.TableName
AFTER | INSTEAD OF [Insert, Update, (and or) Delete]
AS Trigger Code;

You can also create, view and modify triggers using Management Studio’s Object Explorer, as shown in Figure 4-5.
Figure 4-5: Object Explorer lists all triggers for any table and may be used to modify the trigger using the context menu.

Although you can use Enterprise Manager to create and modify triggers, you will need to use Transact-SQL statements within Enterprise Manager to specify your options.

Creating triggers with Transact-SQL

To create a trigger with Transact-SQL, you will use the CREATE TRIGGER Transact-SQL statement.
The syntax for this statement is shown in the following SlideShow:

Create Trigger In the next lesson, you will learn how to create triggers that satisfy unique business rules.