Triggers & Stored
Procedures
In DBMS
Stored Procedure
• A stored procedure in DBMS is a precompiled collection of SQL
statements (and optional procedural logic) that is stored in the
database and can be executed whenever needed.
• Think of it like a ready-made recipe stored in the kitchen (database)
— instead of writing the same SQL commands repeatedly, you just call
the recipe’s name, and the database executes it for you.
Basic Syntax
CREATE PROCEDURE procedure_name
@parameter_name datatype,
@parameter_name datatype
AS
BEGIN
-- SQL statements
END;
Example
Explanation:
CREATE PROCEDURE GetEmployeesByDept • CREATE PROCEDURE → defines
@DeptID INT the procedure.
AS • Parameters (@DeptID) → allow
BEGIN input customization.
SELECT EmpID, Name, Salary
• EXEC → runs the stored
FROM Employees
procedure.
WHERE DepartmentID = @DeptID;
END;
-- Calling the procedure
EXEC GetEmployeesByDept 2;
Trigger
• Trigger is a statement that a system executes
automatically when there is any modification to the
database.
• In a trigger, we first specify when the trigger is to be
executed and then the action to be performed when the
trigger executes.
• Triggers are used to specify certain integrity constraints
and referential constraints that cannot be specified
using the constraint mechanism of SQL.
Think of it like a "database alarm system" — whenever something
happens (insert, update, delete), the trigger reacts automatically.
Key Points
• Event-driven: Runs only when the specified event occurs.
• Automatic execution: You don’t have to call it manually like a stored
procedure.
• Used for:
• Enforcing business rules
• Maintaining audit logs
• Preventing invalid transactions
• Synchronizing tables
Basic Syntax
CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
BEGIN
-- SQL statements to execute
END;
Suppose you want to keep track of all
employee salary changes:
• Explanation:
CREATE TRIGGER salary_update_audit • AFTER UPDATE → Trigger runs
AFTER UPDATE ON employees after the update is completed.
FOR EACH ROW • OLD → Values before the update.
BEGIN • NEW → Values after the update.
INSERT INTO
salary_audit(employee_id, old_salary,
new_salary, change_date)
VALUES (OLD.emp_id, OLD.salary,
NEW.salary, NOW());
END;
For online compiler
CREATE TRIGGER salary_update_audit
AFTER UPDATE ON customers
FOR EACH ROW
BEGIN
INSERT INTO salary_audit(employee_id, old_salary, new_salary,
change_date)
VALUES (OLD.emp_id, OLD.salary, NEW.salary, NOW());
END;
For mysql
DELIMITER $$
CREATE TRIGGER salary_update_audit
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
INSERT INTO salary_audit(employee_id, old_salary, new_salary, change_date)
VALUES (OLD.emp_id, OLD.salary, NEW.salary, NOW());
END$$
DELIMITER ;
To turn off safe mode
SET SQL_SAFE_UPDATES=0;
TO TURN on safe mode
SET SQL_SAFE_UPDATES=1;
Types of Triggers
• 1. Based on Timing (When They
Execute)
Type Description Example Use
Executes before the Validate or modify
BEFORE Trigger triggering event (INSERT, incoming data before
UPDATE, DELETE) occurs. saving.
Executes after the
AFTER Trigger triggering event completes Logging changes, sending
successfully. notifications.
Handle INSERT/UPDATE on
Executes in place of the complex views by
INSTEAD OF Trigger triggering event. Mostly modifying underlying
used with views. tables manually.
2. Based on Event (What Causes
Them to Fire)
Event Description
INSERT Trigger Fires when a new row is inserted.
UPDATE Trigger Fires when an existing row is updated.
DELETE Trigger Fires when a row is deleted.
3. Based on Row or Statement Level
Level Description
Executes once for each row
Row-level Trigger affected by the event. (FOR EACH
ROW)
Executes once for the whole
Statement-level Trigger statement, regardless of how
many rows are affected.
Difference between Triggers &
Stored Procedures
Feature Stored Procedure Trigger
Executes automatically in
Must be called explicitly by response to a specific event
Execution the user or application using (INSERT, UPDATE, DELETE) on
EXEC or as part of a query. a table/view.
When it runs Runs only when called. Runs immediately when the
event happens.
Parameters Can accept input and output Cannot accept parameters.
parameters.