MySQL triggers are database objects that can automatically execute SQL statements in response to specific events, such as inserts, updates, and deletes. They are useful for automating tasks and enforcing data integrity. In this blog post, we'll go over how to create and use MySQL triggers with code examples.
To create a trigger, use the CREATE TRIGGER statement followed by the trigger name, trigger event, and trigger action. Here's an example of a trigger that logs all inserts into a table:
CREATE TRIGGER log_inserts
AFTER INSERT ON my_table
FOR EACH ROW
BEGIN
INSERT INTO log_table (message) VALUES ('New record inserted into my_table');
END
This trigger is set to execute after each insert on my_table, and it inserts a log message into log_table.
Triggers can also be used to automatically update data in response to certain events. Here's an example of a trigger that updates a user's account balance after a transaction:
CREATE TRIGGER update_balance
AFTER INSERT ON transactions
FOR EACH ROW
BEGIN
UPDATE accounts
SET balance = balance + NEW.amount
WHERE id = NEW.account_id;
END
This trigger is set to execute after each insert on the transactions table. It updates the balance field of the corresponding account in the accounts table with the transaction amount.
Triggers can also be used to enforce data integrity by preventing invalid data from being inserted, updated, or deleted. Here's an example of a trigger that prevents an employee from being deleted if they are the last employee in the department:
CREATE TRIGGER prevent_delete
BEFORE DELETE ON employees
FOR EACH ROW
BEGIN
DECLARE emp_count INT;
SELECT COUNT(*) INTO emp_count FROM employees WHERE department = OLD.department;
IF emp_count = 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cannot delete last employee in department';
END IF;
END
This trigger is set to execute before each delete on the employees table. It checks if the employee being deleted is the last employee in their department and raises an error if it is.
Triggers can also be used for auditing purposes, by logging all changes made to a table. Here's an example of a trigger that logs all updates to a users table:
CREATE TRIGGER log_updates
AFTER UPDATE ON users
FOR EACH ROW
BEGIN
INSERT INTO audit_table (user_id, field, old_value, new_value)
VALUES (OLD.id, 'username', OLD.username, NEW.username);
END
This trigger is set to execute after each update on the users table. It inserts a log message into audit_table with the old and new values of the updated username field.
MySQL triggers are powerful tools for automating tasks and enforcing data integrity. By creating triggers, you can automatically execute SQL statements in response to specific events, update data, prevent invalid data from being inserted or deleted, and log all changes made to a table. With the examples provided in this blog post, you can start using triggers in your own MySQL database applications.
Categories : MySQL
Tags : MySQL automation SQL database triggers Data integrity auditing