
If you’ve ever wondered how databases quietly perform actions behind the scenes—almost like they have a mind of their own—you’re already thinking about something called a trigger. And honestly, understanding triggers is one of those moments where SQL starts feeling less like a query language and more like a smart assistant that reacts on its own.
But let’s start at the very beginning.
A trigger in SQL is a special stored program that automatically runs when a specific event happens in a table—like an insert, update, or delete.
Think of it as a “database reaction.”
Something changes → the trigger fires → an action happens.
No button clicks. No manual execution. It just… happens.
(And why you’ll eventually appreciate them)**
Triggers help with things that must stay consistent every single time. You know how people forget to update logs or recalculate totals? Well, triggers don’t forget.
They’re used for:
It’s like having a silent guard watching your data.
We’ll walk through them naturally—because SQL terms can feel heavy unless someone breaks them down with some common sense.
This runs before an operation happens.
Imagine you're about to insert a record, and the database pauses and says,
“Hold on… let me check something first.”
You might use a BEFORE trigger to:
Example:
Prevent negative salary values before insertion.
This runs after the operation is completed.
So the database only reacts once it’s sure the insert/update/delete actually happened.
Useful for:
Example:
After creating an order, automatically insert the order log.
This one replaces the original operation entirely.
It’s mainly used in views, especially when the view doesn’t allow direct changes.
You know what? It’s like telling the database,
“Don’t do what you were going to do—do this instead.”
Example:
Updating two underlying tables when a view is updated.
Row-Level Trigger
Fires for each row affected.
If 10 rows are inserted, it fires 10 times.
It feels like checking every student in a classroom one by one.
Statement-Level Trigger
Fires once per SQL statement, regardless of the number of rows.
Feels more like making one announcement for the whole group.
Triggers respond to three main events:
Some databases also support additional events like:
But for beginners, focusing on INSERT/UPDATE/DELETE is more than enough.
Let’s imagine you have a table named employees.
When someone inserts a new employee, you want to automatically update another table called audit_log.
A trigger lets you do this:
AFTER INSERT ON employees
→ insert record into audit_log
You don’t run this manually.
You don’t even see it happen.
The moment the insert finishes, the trigger steps in—like an automatic camera capturing every move.
Here’s an example of an AFTER INSERT trigger:
CREATE TRIGGER after_employee_insert
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO audit_log(action, employee_id, time_stamp)
VALUES ('New Employee Added', NEW.id, NOW());
END;
Notice NEW.id?
It refers to values of the newly inserted row.
Similarly, in DELETE triggers, you have the OLD keyword.
Freshers often ask, “Will companies expect me to write triggers a lot?”
Here’s the thing:
Triggers are used when:
Industries like banking, insurance, healthcare, and finance rely on triggers heavily because data accuracy is non-negotiable.
So yes—triggers are powerful, but they should be used thoughtfully.
Call Aimore Technologies today for expert SQL training course and get the professional training you need.