⬅ Previous Topic
SQL User Defined Functions (UDF)Next Topic ⮕
SQL Transactions – COMMIT, ROLLBACK, SAVEPOINT⬅ Previous Topic
SQL User Defined Functions (UDF)Next Topic ⮕
SQL Transactions – COMMIT, ROLLBACK, SAVEPOINTImagine you want to automatically log when a student's result is updated — without writing extra SQL every time. That’s exactly what triggers do. They’re special procedures that run in response to events like INSERT
, UPDATE
, or DELETE
— and they do so automatically.
A trigger is a named set of SQL statements that automatically executes when a specific table event occurs. Triggers help with auditing, validation, or enforcing rules behind the scenes.
CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
BEGIN
-- logic here
END;
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE results (
roll_no INT,
subject VARCHAR(30),
marks INT
);
CREATE TABLE result_audit (
audit_id INT AUTO_INCREMENT PRIMARY KEY,
roll_no INT,
subject VARCHAR(30),
old_marks INT,
new_marks INT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
This trigger logs any change to marks
in the results
table into the result_audit
table.
DELIMITER $$
CREATE TRIGGER after_marks_update
AFTER UPDATE ON results
FOR EACH ROW
BEGIN
IF OLD.marks != NEW.marks THEN
INSERT INTO result_audit (roll_no, subject, old_marks, new_marks)
VALUES (OLD.roll_no, OLD.subject, OLD.marks, NEW.marks);
END IF;
END$$
DELIMITER ;
UPDATE results
SET marks = 90
WHERE roll_no = 1 AND subject = 'Maths';
audit_id | roll_no | subject | old_marks | new_marks | updated_at
---------+---------+---------+-----------+-----------+---------------------
1 | 1 | Maths | 85 | 90 | 2024-05-24 10:12:33
Prevent insertion of marks greater than 100:
DELIMITER $$
CREATE TRIGGER check_marks_before_insert
BEFORE INSERT ON results
FOR EACH ROW
BEGIN
IF NEW.marks > 100 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Marks cannot exceed 100';
END IF;
END$$
DELIMITER ;
INSERT INTO results VALUES (2, 'Science', 105);
ERROR 1644 (45000): Marks cannot exceed 100
Stop deletion of results for a topper:
DELIMITER $$
CREATE TRIGGER protect_topper
BEFORE DELETE ON results
FOR EACH ROW
BEGIN
IF OLD.marks >= 90 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot delete result of a topper';
END IF;
END$$
DELIMITER ;
before_insert_check
, log_updates
).audit
tableTriggers
act like silent protectors in your database — enforcing rules, logging changes, and blocking bad data — all without writing manual queries every time. For school databases or large systems, triggers help maintain consistency and automate monitoring.
Coming up: SQL Transactions — group multiple statements into a single, all-or-nothing operation.
CREATE TRIGGER trg_log_update
AFTER UPDATE ON students
FOR EACH ROW
BEGIN
INSERT INTO log_table(student_id, action_time)
VALUES (NEW.id, CURRENT_TIMESTAMP);
END;
What is the purpose of this trigger?⬅ Previous Topic
SQL User Defined Functions (UDF)Next Topic ⮕
SQL Transactions – COMMIT, ROLLBACK, SAVEPOINTYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.