Triggers in SQL
A trigger is a set of SQL statements that are automatically executed in response to a specific event, such as an insert, update, or delete operation. Triggers can be used to enforce data integrity, audit changes, or perform complex business logic. Here's an example:
sql
CREATE TRIGGER update_employee_salary
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
UPDATE salaries
SET amount = amount * 1.1
WHERE employee_id = NEW.id;
END;
In this example, we're creating a trigger called "update_employee_salary" that is executed after an update operation on the "employees" table. This trigger updates the salaries of all employees with a 10% raise whenever their information is updated in the "employees" table.
No comments:
Post a Comment