SQL
TRIGGERS
GROUP 3 - BSIT 2R13
WHAT ARE SQL TRIGGERS?
SQL SQL TRIGGER
Structured Query Are stored procedures
Language for that automatically
managing relational execute in response to
databases. certain events.
SQL TRIGGER
A trigger is a set of SQL statements that execute
automatically in response to certain table events.
Triggers enable automatic responses to data changes,
ensuring consistency and reducing the need for
application-side logic.
PURPOSE & BENEFITS OF
TRIGGERS
Automate Improve
Maintain
System Data
Audit Trails
Tasks Integrity
SQL Trigger Syntax
Components:
‘BEFORE` / `AFTER` / `INSTEAD OF`: Timing of execution.
`INSERT`, `UPDATE`, `DELETE`: Event type.
`FOR EACH ROW`: Row-level execution.
Types of Triggers
1. TRIGGERS BASED ON TIMING
BEFORE AFTER INSTEAD OF
Executes before the Executes after the Replaces the normal
triggering event. triggering event. execution of the event.
Used to validate or Used for logging or Mainly used on views to
modify incoming data. post-processing tasks. customize behavior.
Types of Triggers
1. TRIGGERS BASED ON EVENTS
INSERT UPDATE DELETE
Executes when a new Executes when an Executes when a row is
row is added to a table. existing row is modified. removed from a table.
Useful for initializing Commonly used for Often used to archive
values or updating tracking changes or data or maintain
related records. enforcing business logic. referential integrity.
Automate Repetitive
01 Tasks
Track and Log Data
02
WHEN TO USE Changes
TRIGGERS? Enforce Business Rules
03
Beyond Constraints
04 Maintain Data
Consistency Across
Tables
Trigger Example
Automatically Updating Product Stock After a Sale (AFTER
INSERT Trigger)
THANK YOU!