Day 32: Triggers
Introduction:
Triggers are database objects that automatically execute a specified set of SQL statements when certain events occur, such as
INSERT,UPDATE, orDELETEoperations. They are used to enforce rules, automate tasks, and maintain data integrity.
Key Concepts:
Trigger: A set of SQL statements that automatically executes in response to specific events.
CREATE TRIGGER: The SQL statement used to create a trigger.
BEFORE and AFTER: Specifies when the trigger should execute relative to the triggering event.
FOR EACH ROW: Indicates that the trigger operates on each row affected by the event.
SQL Commands and Examples:
Creating a Trigger:
Define a trigger that executes before an
INSERToperation.
-- Create a trigger that logs new student insertions
CREATE TRIGGER log_student_insert
BEFORE INSERT ON Students
FOR EACH ROW
BEGIN
INSERT INTO StudentLogs (StudentID, Action, ActionTime)
VALUES (NEW.StudentID, 'INSERT', NOW());
END;
Trigger with UPDATE Operation:
Define a trigger that executes after an
UPDATEoperation.
-- Create a trigger that logs student updates
CREATE TRIGGER log_student_update
AFTER UPDATE ON Students
FOR EACH ROW
BEGIN
INSERT INTO StudentLogs (StudentID, Action, ActionTime)
VALUES (NEW.StudentID, 'UPDATE', NOW());
END;
Trigger with DELETE Operation:
Define a trigger that executes before a
DELETEoperation.
-- Create a trigger that logs student deletions
CREATE TRIGGER log_student_delete
BEFORE DELETE ON Students
FOR EACH ROW
BEGIN
INSERT INTO StudentLogs (StudentID, Action, ActionTime)
VALUES (OLD.StudentID, 'DELETE', NOW());
END;
Practice Exercise:
Use the Students table and create a new StudentLogs table with columns
LogID,StudentID,Action,ActionTime.Write a query to create a trigger that logs new student insertions.
Write a query to create a trigger that logs student updates.
Write a query to create a trigger that logs student deletions.
-- Create the StudentLogs table
CREATE TABLE StudentLogs (
LogID INT AUTO_INCREMENT PRIMARY KEY,
StudentID INT,
Action VARCHAR(50),
ActionTime DATETIME
);
-- Create a trigger that logs new student insertions
CREATE TRIGGER log_student_insert
BEFORE INSERT ON Students
FOR EACH ROW
BEGIN
INSERT INTO StudentLogs (StudentID, Action, ActionTime)
VALUES (NEW.StudentID, 'INSERT', NOW());
END;
-- Create a trigger that logs student updates
CREATE TRIGGER log_student_update
AFTER UPDATE ON Students
FOR EACH ROW
BEGIN
INSERT INTO StudentLogs (StudentID, Action, ActionTime)
VALUES (NEW.StudentID, 'UPDATE', NOW());
END;
-- Create a trigger that logs student deletions
CREATE TRIGGER log_student_delete
BEFORE DELETE ON Students
FOR EACH ROW
BEGIN
INSERT INTO StudentLogs (StudentID, Action, ActionTime)
VALUES (OLD.StudentID, 'DELETE', NOW());
END;
Important Tips:
Triggers can help maintain data integrity and enforce business rules automatically.
Be cautious with triggers, as they can introduce complexity and affect performance.
Use appropriate trigger types (
BEFOREorAFTER) based on your requirements.
Understanding triggers allows you to automate tasks and enforce rules at the database level.
0 Comments