A trigger is a mini-program welded to a table event. When the event happens, the database runs it automatically — no one has to remember to call it.
1 CREATE TRIGGER
CREATE TRIGGER name AFTER UPDATE ON table attaches the program to that event.
2 BEFORE or AFTER
BEFORE runs first — handy for checks; AFTER runs after — handy for logs.
3 FOR EACH ROW
If one statement touches five rows, the trigger fires five times, once per row.
4 OLD and NEW
Inside the trigger, OLD is the row before the change and NEW is the row after.
SQL that fires itself
CREATE TRIGGER1CREATE TRIGGER name AFTER UPDATE ON table attaches the program to that event. BEFORE or AFTER2BEFORE runs first — handy for checks; AFTER runs after — handy for logs. FOR EACH ROW3If one statement touches five rows, the trigger fires five times, once per row. OLD and NEW4Inside the trigger, OLD is the row before the change and NEW is the row after. 4 building blocks make up SQL that fires itself
Press play: the trigger is armed first, then a plain UPDATE on employees makes it fire — nobody calls it.
CREATE TRIGGER welds a mini-program onto an event of a table. Nobody calls it — events do.
Memory
no variables yet — press play
The change lands first — then the attached program fires, all inside the same statement.
process decision input / output start / end Click a term, then click its description to lock the pair.
5 conceptsTap a card to expand ?Triggerdrops into place
?BEFORE / AFTERdrops into place
?FOR EACH ROWdrops into place
?OLD and NEWdrops into place
?Audit logdrops into place
An INSERT arrives on a table with an AFTER INSERT trigger. What does the client report?
Your predicted output — one line per console line
Hint: The INSERT touches one row — and the AFTER INSERT trigger adds exactly one audit row for it.
01A trigger is SQL the database runs for you when an event happens.
02BEFORE suits validation; AFTER suits logging and follow-up writes.
03FOR EACH ROW means one firing per changed row.
04OLD and NEW give the trigger both sides of the change.
05Perfect for audit trails and rules nobody is allowed to forget.