SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
SQL Triggers are used to automatically perform an action in response to specific events on a particular table or view in a database. They are typically used for maintaining the integrity of the information in the database.
Let's say you have a student database with a Students
table and an AuditLog
table. You want to maintain a log whenever a new student is added or whenever a student's data is updated or deleted. You can use triggers to achieve this.
Here's a basic example:
Database Tables:
Students:
AuditLog:
Triggers:
After INSERT on Students:
CREATE TRIGGER tr_students_after_insert AFTER INSERT ON Students FOR EACH ROW BEGIN INSERT INTO AuditLog (Action, TableName, RecordID, Description, ActionDate) VALUES ('INSERT', 'Students', NEW.StudentID, CONCAT('Inserted student with ID ', NEW.StudentID), NOW()); END;
After UPDATE on Students:
CREATE TRIGGER tr_students_after_update AFTER UPDATE ON Students FOR EACH ROW BEGIN INSERT INTO AuditLog (Action, TableName, RecordID, Description, ActionDate) VALUES ('UPDATE', 'Students', NEW.StudentID, CONCAT('Updated student with ID ', NEW.StudentID), NOW()); END;
After DELETE on Students:
CREATE TRIGGER tr_students_after_delete AFTER DELETE ON Students FOR EACH ROW BEGIN INSERT INTO AuditLog (Action, TableName, RecordID, Description, ActionDate) VALUES ('DELETE', 'Students', OLD.StudentID, CONCAT('Deleted student with ID ', OLD.StudentID), NOW()); END;
Note:
NEW
and OLD
are keywords used in triggers to refer to the new and old values of the record.NOW()
is a function to get the current timestamp. Depending on your SQL server, the exact function might differ (e.g., GETDATE()
for SQL Server).Always be cautious when using triggers. They can be powerful, but they also have the potential to cause hard-to-debug issues if not used judiciously. Always document them well, and ensure that database administrators and developers are aware of them.
SQL Trigger example for student database:
CREATE TRIGGER AfterStudentInsert ON StudentTable AFTER INSERT AS BEGIN -- Trigger logic here -- e.g., Log the insert operation END;
Using Triggers to enforce constraints in a student table:
CREATE TRIGGER EnforceGPAConstraint ON StudentTable INSTEAD OF INSERT AS BEGIN -- Trigger logic here -- e.g., Check GPA constraint before allowing insert END;
Trigger for auditing changes in a student database:
CREATE TRIGGER AuditStudentChanges ON StudentTable AFTER UPDATE AS BEGIN -- Trigger logic here -- e.g., Log changes to an audit table END;
Automating updates with SQL Triggers in student records:
CREATE TRIGGER UpdateLastModified ON StudentTable AFTER UPDATE AS BEGIN -- Trigger logic here -- e.g., Update LastModified column with current timestamp END;
Student database notifications using SQL Triggers:
CREATE TRIGGER NotifyStudentUpdate ON StudentTable AFTER UPDATE AS BEGIN -- Trigger logic here -- e.g., Send notification to relevant parties END;
How to implement a BEFORE INSERT Trigger in student database:
CREATE TRIGGER BeforeStudentInsert ON StudentTable BEFORE INSERT AS BEGIN -- Trigger logic here -- e.g., Validate data before insert END;
AFTER UPDATE Trigger for maintaining student history:
CREATE TRIGGER StudentHistoryUpdate ON StudentTable AFTER UPDATE AS BEGIN -- Trigger logic here -- e.g., Insert a record into StudentHistory table END;
SQL Trigger for cascading changes in student-related tables:
CREATE TRIGGER CascadeStudentUpdate ON StudentTable AFTER UPDATE AS BEGIN -- Trigger logic here -- e.g., Update related tables END;
Preventing invalid deletions with a DELETE Trigger in student records:
CREATE TRIGGER PreventInvalidDeletion ON StudentTable INSTEAD OF DELETE AS BEGIN -- Trigger logic here -- e.g., Check conditions before allowing delete END;
Ensuring data consistency with SQL Triggers in student management:
CREATE TRIGGER EnforceDataConsistency ON StudentTable AFTER INSERT, UPDATE, DELETE AS BEGIN -- Trigger logic here -- e.g., Enforce data consistency rules END;