Ad space (banner)
🗄️SQL Lessons
Lesson 32 / 50

The Basics of TRIGGER

This lesson introduces TRIGGER — automatic actions that fire in response to a data change. It's for anyone searching "SQL TRIGGER tutorial."

A trigger automatically runs another action whenever an INSERT or UPDATE (and similar events) happen — useful for something like "automatically log a history entry whenever inventory changes."

The example creates a trigger named stock_changed that automatically writes to item_logs every time items is updated. After updating the stock, check that a log entry was written automatically.

A common early mistake is confusing the special OLD and NEW keywords — OLD refers to the value before the change, and NEW to the value after, and you need to pick the right one depending on what your trigger is doing. The upside is real: the database logs the history on its own, without the application ever having to remember to write that code, which prevents missed log entries — a pattern widely used for audit logs and notification systems, and one that starts to feel like a more advanced tool as you get comfortable with it.

SQL
OUTPUT

💡 The SQL engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)