Chapter9 SQL
Chapter9 SQL
B U I L D I N G A N D O P TI M I Z I N G TR I G G E R S I N S Q L S E R V E R
Etibar Vazirov
Slides credit: Florin Angelescu
What is a trigger?
Special type of stored procedure
Prevent deletions
Etibar
FlorinVazirov
Angelescu
Slides credit: Florin Angelescu
Instructor
Why should we use DML triggers?
Initiating actions when manipulating data
Initial event fires the trigger Initial event fires the trigger
Etibar
FlorinVazirov
Angelescu
Slides credit: Florin Angelescu
Instructor
Triggers vs. stored procedures
Triggers Stored procedures
Fired automatically by an event Run only when called explicitly
use columns from other tables for use columns only from the same table for
calculations calculations
INSERT or UPDATE used to calculate calculation defined when creating the table
-- Column definition
-- Used in the trigger body
[...]
[...]
TotalAmount AS Price * Quantity
UPDATE
[...]
SET TotalAmount = Price * Quantity
[...]