0
0
SQLquery~10 mins

BEFORE trigger execution in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - BEFORE trigger execution
Start: Insert/Update/Delete operation
BEFORE Trigger fires
Trigger code runs: can modify NEW row
Original operation continues with possibly modified data
Operation completes, AFTER triggers may run
When a data change is requested, the BEFORE trigger runs first, allowing changes before the actual data operation happens.
Execution Sample
SQL
CREATE TRIGGER before_insert_example
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
  SET NEW.salary = NEW.salary * 1.1;
END;
This trigger increases the salary by 10% before inserting a new employee row.
Execution Table
StepActionNEW.salary beforeTrigger modifies NEW.salaryNEW.salary afterOperation continues
1Insert employee with salary 1000 requestedN/ANoN/AWaiting for trigger
2BEFORE INSERT trigger fires1000Yes, multiply by 1.11100Trigger ends
3Insert operation continues with NEW.salary=11001100No1100Row inserted with updated salary
💡 Trigger finishes, operation continues with modified NEW data
Variable Tracker
VariableStartAfter Step 1After Step 2Final
NEW.salaryN/A100011001100
Key Moments - 2 Insights
Why can the NEW.salary value be changed inside the BEFORE trigger?
Because the BEFORE trigger runs before the actual insert, it can modify the NEW row values that will be saved, as shown in execution_table step 2.
Does the original insert operation use the old or modified NEW.salary?
It uses the modified NEW.salary after the trigger runs, as shown in execution_table step 3 where the operation continues with salary 1100.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is NEW.salary after the BEFORE trigger modifies it?
A1000
B1100
C900
D1200
💡 Hint
Check the 'NEW.salary after' column in step 2 of the execution_table.
At which step does the actual insert operation use the modified salary?
AStep 1
BStep 2
CStep 3
DAfter all steps
💡 Hint
Look at the 'Operation continues' column in step 3 of the execution_table.
If the BEFORE trigger did not modify NEW.salary, what would the final salary be?
A1000
B1100
C0
DNULL
💡 Hint
Refer to the 'NEW.salary before' value in step 2 and imagine no modification.
Concept Snapshot
BEFORE triggers run before data changes happen.
They can modify NEW row data.
Changes affect the actual operation.
Useful for validation or automatic adjustments.
Runs once per affected row before insert/update/delete.
Full Transcript
When you insert, update, or delete data, a BEFORE trigger runs first. It can change the data before it is saved. For example, if you insert a salary of 1000, the BEFORE trigger can increase it to 1100. Then the insert uses this new value. This lets you adjust or check data automatically before saving. The execution table shows the steps: request insert, trigger runs and changes salary, then insert continues with new salary.