TRIGGER
A trigger in SQL is a set of procedural statements that are automatically executed in response to certain events on a particular table in the database. Triggers are used to maintain data integrity and enforce business rules by specifying actions that should occur when specific changes are made to the data1 2 .
Types of Triggers
There are six main types of triggers in SQL:
Example of a Trigger
Here is an example of a trigger that ensures no employee under the age of 25 can be inserted into the database:
delimiter $$
CREATE TRIGGER Check_age BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
IF NEW.age < 25 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ERROR: AGE MUST BE AT LEAST 25 YEARS!';
END IF;
END; $$
delimiter ;
In this example, the trigger named Check_age is executed before inserting any tuple into the employee table. It checks the age attribute, and if it is less than 25, it raises an error1 .
Advantages and Disadvantages
Advantages
Disadvantages
Triggers are a powerful feature in SQL that can help automate tasks and enforce data integrity, but they should be used judiciously to avoid potential performance and complexity issues.