Microsoft® SQL Server™ 2008 All-In-One Desk Reference For Dummies®
by Robert D. Schneider, Darril Gibson
IV.3.4. Creating Triggers
Triggers are created using T-SQL code. Although they're viewable within the SQL Server Management Studio (SSMS) after they've been created, you can't create them in the graphical user interface.
IV.3.4.1. Creating a DML trigger
The following basic syntax creates a DML trigger:
CREATE TRIGGER triggername ON table or view AFTER or INSTEAD OF INSERT or UPDATE or DELETE AS trigger code
The trigger will be created in the current database. Because the default database is Master when you open SSMS, you would usually change the context of the database with a USE command immediately preceding the trigger code.
In the following steps, you create a Sales database, a Customers table, an UPDATE trigger, and a DELETE trigger. By manipulating the data within the Customers table, you'll see the triggers being fired.
Launch SQL Server Management Studio.
Choose Start
All Programs
Microsoft SQL Server 2008
SQL Server Management Studio.Open a new query window by clicking the New Query button.
Enter and execute the following code to create a database named Sales with a table named Customers:
USE Master; GO CREATE DATABASE Sales; GO USE Sales; GO CREATE TABLE Customers ( CustomerID int ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access