Introduction to Triggers in SQL Programming Language
Triggers are an essential part of SQL programming, acting as automatic responses to specific events that occur in a database, are an important aspect of
Triggers are an essential part of SQL programming, acting as automatic responses to specific events that occur in a database, are an important aspect of
A trigger is a special kind of stored procedure that runs automatically (“fires”) when particular events occur in a database table. Different from regular stored procedures, which can be explicitly called or executed by users, triggers can be triggered only by special actions like INSERT, UPDATE, or DELETE applied to a table.
Trigger can be used to:
The SQL database triggers have two classifications based on the events that cause them to fire and their timing of execution.
Row-level triggers are executed once for each row affected by a triggering event. This means that if a statement impacts multiple rows, the trigger will fire separately for each row.
Example: Row-Level Trigger
We have a table employees with some employee data in it, including an employee’s salary. We want to record the change to the employee’s salary each time the salary of an employee is changed in the salary_changes table.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
salary DECIMAL(10, 2)
);
CREATE TABLE salary_changes (
change_id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT,
old_salary DECIMAL(10, 2),
new_salary DECIMAL(10, 2),
change_time DATETIME
);
CREATE TRIGGER log_salary_change
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
IF OLD.salary <> NEW.salary THEN
INSERT INTO salary_changes (employee_id, old_salary, new_salary, change_time)
VALUES (NEW.employee_id, OLD.salary, NEW.salary, NOW());
END IF;
END;
How It Works:
UPDATE
statement:UPDATE employees
SET salary = salary * 1.10; -- Assuming this updates 3 employees
The trigger log_salary_change, therefore, will be called three times, with three records being inserted into the salary_changes table each containing the old and new salaries for those employees affected.
Statement-level triggers execute once for the entire SQL statement, regardless of how many rows are affected.
Triggers can be categorized into row-level and statement-level triggers based on the scope of their action.
UPDATE
statement affects 10 rows, the associated row-level trigger will execute 10 times, once for each row.UPDATE
statement affects multiple rows, the statement-level trigger runs only once.Example: Statement-Level Trigger
We have a requirement to keep log records of all updates on the employees table, but we want to do this only once per statement, not matter how many rows are affected.
CREATE TABLE update_log (
log_id INT AUTO_INCREMENT PRIMARY KEY,
update_time DATETIME,
total_updated INT
);
CREATE TRIGGER log_employee_updates
AFTER UPDATE ON employees
FOR EACH STATEMENT
BEGIN
INSERT INTO update_log (update_time, total_updated)
VALUES (NOW(), ROW_COUNT());
END;
How It Works:
UPDATE
:UPDATE employees
SET salary = salary * 1.10; -- Updates multiple employees
log_employee_updates
trigger fires only once, logging a single entry in the update_log
table with the timestamp and the number of rows updated (which can be accessed via ROW_COUNT()
).Before triggers are executed before the actual triggering action (like INSERT
, UPDATE
, or DELETE
) takes place. This is useful for validating data or modifying it before it is saved to the database.
Triggers can also be categorized based on when they are executed relative to the triggering event.
INSERT
or UPDATE
operation.Example: Before Trigger
Let’s ensure that no employee can be added with a salary lower than 30,000.
CREATE TRIGGER check_min_salary
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < 30000 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Salary cannot be less than 30,000.';
END IF;
END;
How It Works:
INSERT INTO employees (employee_id, employee_name, salary)
VALUES (1, 'John Doe', 25000); -- This will fail
check_min_salary
trigger will fire and raise an error, preventing the insert from occurring.After triggers are executed after the triggering action has completed. They can be used for logging, updating related tables, or performing actions that depend on the successful completion of the primary operation.
Example: After Trigger
Let’s create a trigger that logs when a new employee is added:
CREATE TABLE employee_log (
log_id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT,
action VARCHAR(50),
action_time DATETIME
);
CREATE TRIGGER log_new_employee
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employee_log (employee_id, action, action_time)
VALUES (NEW.employee_id, 'Inserted', NOW());
END;
How It Works:
INSERT INTO employees (employee_id, employee_name, salary)
VALUES (2, 'Jane Smith', 35000);
log_new_employee
trigger will fire after the insert completes, recording the action in the employee_log
table.INSTEAD OF
triggers are used to replace the standard actions of INSERT
, UPDATE
, or DELETE
on views, allowing you to customize how these operations are handled.
These triggers are a special type that replaces the usual action for a triggering event. For example, an INSTEAD OF
trigger can be used on a view to redirect insert, update, or delete operations to the underlying tables, thereby controlling how data modifications are processed.
Example: INSTEAD OF Trigger
Suppose you have a view summarizing employee information. You’d like to allow updates through the view, but in reality, it will update the underlying table instead.
CREATE VIEW employee_view AS
SELECT employee_id, employee_name, salary FROM employees;
CREATE TRIGGER update_employee_view
INSTEAD OF UPDATE ON employee_view
FOR EACH ROW
BEGIN
UPDATE employees
SET salary = NEW.salary
WHERE employee_id = NEW.employee_id;
END;
How It Works:
UPDATE
on the view:UPDATE employee_view
SET salary = 40000
WHERE employee_id = 2;
update_employee_view
trigger will fire, and the salary in the employees
table for the specified employee will be updated, even though the action was performed on the view.To create a trigger in SQL, you typically use the CREATE TRIGGER
statement, followed by the trigger name, the timing (BEFORE or AFTER), the event (INSERT, UPDATE, or DELETE), and the table it’s associated with.
CREATE TRIGGER trigger_name
{ BEFORE | AFTER | INSTEAD OF }
{ INSERT | UPDATE | DELETE }
ON table_name
FOR EACH ROW
BEGIN
-- SQL statements to execute
END;
Let’s create a trigger that logs changes to an employees
table whenever a record is updated. This trigger will insert a record into a change_log
table containing details of the change.
CREATE TABLE change_log (
id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT,
change_time DATETIME,
old_salary DECIMAL(10, 2),
new_salary DECIMAL(10, 2)
);
CREATE TRIGGER LogSalaryChange
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
IF OLD.salary <> NEW.salary THEN
INSERT INTO change_log (employee_id, change_time, old_salary, new_salary)
VALUES (NEW.employee_id, NOW(), OLD.salary, NEW.salary);
END IF;
END;
LogSalaryChange
is the name of the trigger.AFTER UPDATE
trigger, meaning it executes after an update occurs on the employees
table.salary
of the employee has changed (comparing OLD.salary
with NEW.salary
). If there’s a change, it inserts a record into the change_log
table, capturing the employee’s ID, the timestamp of the change, and both the old and new salary values.Consider a scenario where you want to ensure that no employee can have a salary less than a certain threshold. You can create a BEFORE INSERT
trigger to enforce this rule:
CREATE TRIGGER EnsureMinSalary
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < 30000 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Salary cannot be less than 30,000.';
END IF;
END;
EnsureMinSalary
is the name of the trigger.BEFORE INSERT
trigger, which means it runs before a new employee record is inserted into the employees
table.NEW.salary
). If it’s below 30,000, it raises an error using the SIGNAL
statement, preventing the insert operation.Triggers are executed automatically by the database system when the specified events occur on the associated table. There is no need for explicit execution. For example, if you perform an UPDATE
on the employees
table, the LogSalaryChange
trigger will fire automatically if the salary changes.
UPDATE employees
SET salary = 35000
WHERE employee_id = 1;
In this case, if the employee with ID 1 had a salary lower than 35,000 before the update, the LogSalaryChange
trigger will log the change in the change_log
table.
You can modify an existing trigger using the DROP TRIGGER
statement followed by CREATE TRIGGER
to redefine it, as SQL does not have a direct ALTER TRIGGER
statement.
To remove a trigger from a table, use the DROP TRIGGER
statement:
DROP TRIGGER LogSalaryChange;
This command will delete the LogSalaryChange
trigger from the database, meaning it will no longer be invoked on updates to the employees
table.
SQL triggers offer numerous benefits by automatically executing predefined actions when specific database events occur. Here are some of the primary advantages of using triggers in SQL:
No Manual Intervention: Triggers are automatically executed when specified database events (INSERT, UPDATE, DELETE) occur. This automation helps ensure that critical tasks are performed without requiring manual execution.
Consistent Data Integrity: Triggers help enforce business rules at the database level by validating data before changes are committed. This ensures that constraints are maintained and invalid data is rejected.
Track Data Changes: Triggers can be used for auditing purposes, allowing you to log changes to data, record timestamps, track users making changes, and maintain a history of all updates, deletions, or insertions in the database.
Consistency in Data Derivation: Triggers help maintain derived data or aggregate values by automatically recalculating or updating values in other tables when data is modified. For example, updating a total amount field when a new order is inserted.
Data Validation Across Multiple Tables: Triggers can validate data across different tables, ensuring that interrelated data in different tables remains consistent, thus avoiding the possibility of orphan records or mismatches.
Advanced Integrity Checking: Triggers allow the enforcement of complex integrity constraints that may not be possible with standard SQL constraints (e.g., complex inter-table relationships or conditional rules).
Enhanced Security Measures: Triggers can restrict unauthorized changes to data by validating and checking data modifications before they are made. This adds a layer of security by ensuring only authorized or valid changes are applied.
Less Application Logic: By embedding certain logic directly into the database through triggers, you reduce the amount of complex validation and integrity code that needs to be written in the application layer, simplifying application development.
Instant Execution: Triggers provide an immediate response to changes in the database, ensuring real-time reaction to critical events (e.g., alerting or updating another system instantly when a sensitive data update occurs).
Automated Workflows: Triggers help avoid repetitive manual actions or workflows. Once defined, they ensure that certain tasks, like recalculations or synchronizations, are handled automatically whenever relevant changes occur in the database.
While triggers provide powerful automation and consistency features in SQL, they also come with several potential drawbacks that can complicate database management. Below are the key disadvantages of using triggers:
Lack of Granular Control: Triggers have limited error-handling capabilities, which makes it difficult to manage exceptions within trigger code. In case of an error, the whole operation might fail without enough detail on what went wrong.
Subscribe to get the latest posts sent to your email.