MySQL AFTER DELETE Trigger
Last Updated :
05 Aug, 2024
In MySQL, triggers are a concept where SQL developers can define automatic actions to occur in response to specific changes in a database table. An AFTER DELETE trigger, for instance, is invoked automatically after a row is deleted from a table, allowing developers to perform additional operations such as logging, data cleanup, or cascading updates.
In this article, We will learn about the MySQL AFTER DELETE Trigger by understanding various examples and so on.
MySQL AFTER DELETE Trigger
- A MySQL AFTER DELETE trigger is a type of database trigger that activates automatically after a row is deleted from a specified table.
- This trigger can be used to perform additional operations, such as logging the deletion, updating related tables, or enforcing business rules, making sure that all required actions occur in response to the delete operation.
Syntax:
The syntax for using AFTER DELETE Trigger in MySQL is as follows:
CREATE TRIGGER trigger_name
AFTER DELETE ON table_name
FOR EACH ROW
BEGIN
-- Trigger actions (e.g., INSERT INTO another_table, UPDATE table_name, etc.)
END;
Parameters
trigger_name
: The name of the trigger.AFTER DELETE ON table_name
: Specifies that the trigger should activate after a delete operation on the specified table.FOR EACH ROW
: Indicates that the trigger will be executed once for each row affected by the delete operation.BEGIN ... END;
: Contains the SQL statements that define the actions to be performed when the trigger is activated.
Example of MySQL AFTER DELETE Trigger
Let’s look at example of the AFTER DELETE Trigger in MySQL. Learning the AFTER DELETE Trigger with examples will help in understanding the concept better.
First, let’s create a table:
We create the table "employees" in this example.
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
position VARCHAR(100) NOT NULL
);
DESCRIBE employees ;
Output
OutputExample: Creating AFTER DELETE Trigger that Performs Logging Delete Operation
In this example, we are creating an AFTER DELETE trigger named after_employee_delete that logs deleted records into the deletion_log table. First, we set up the deletion_log table to track deletions, then insert sample data into the employees table. The trigger is defined to insert details of deleted records into deletion_log when a delete operation occurs. Finally, deleting a record from employees triggers this action, and we verify the log entry by querying the deletion_log table.
Step 1: Firstly we need to create a log table to track the after delete operations.
CREATE TABLE deletion_log (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
action_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
action VARCHAR(50)
);
DESCRIBE deletion_log;
OutputStep 2: Insert some sample data into the employees
table:
INSERT INTO employees (name, position) VALUES ('Gaurav', 'Software Engineer');
INSERT INTO employees (name, position) VALUES ('Yuvraj', 'Project Manager');
OutputStep 3: Now, create a trigger that logs deleted records into the deletion_log
table:
DELIMITER //
CREATE TRIGGER after_employee_delete
AFTER DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO deletion_log (employee_id, old_name, old_position)
VALUES (OLD.id, OLD.name, OLD.position);
END//
DELIMITER ;
Step 4: Delete a record from the employees
table to see the trigger in action. Verify that the deleted record has been logged:
DELETE FROM employees WHERE id = 1;
SELECT * FROM deletion_log;
Output:
OutputConclusion
In conclusion, an AFTER DELETE trigger is useful for automating post-deletion operations, such as logging deleted records for audit purposes or further processing. By setting up this trigger, you make sure that every delete action is tracked and relevant information is recorded, enhancing data integrity.
Similar Reads
MySQL BEFORE DELETE Trigger
MySQL BEFORE DELETE trigger is a powerful tool that automatically performs specific actions before an DELETE operation is executed on a table. This feature allows us to handle tasks such as logging deleted records, enforcing business rules or validating data before it is removed from the database.In
3 min read
MySQL After Insert Trigger
An "AFTER INSERT" trigger in MySQL automatically executes specified actions after a new row is inserted into a table. It is used to perform tasks such as updating related tables, logging changes or performing calculations, ensuring immediate and consistent data processing.In this article, We will le
4 min read
MySQL Create Trigger
Database triggers are specialized procedures that automatically respond to certain events on a table or view. These events include actions such as INSERT, UPDATE, or DELETE. Triggers can be used to enforce complex business rules, maintain audit trails, or synchronize data across tables. In MySQL, tr
4 min read
MySQL AFTER UPDATE Trigger
Triggers in MySQL are special stored programs that are automatically executed when a specific event occurs on the table such as an INSERT, UPDATE, or DELETE. An AFTER UPDATE trigger is a type of trigger that executes after an UPDATE statement is performed on the table. This article provides a comple
4 min read
MySQL Before Insert Trigger
MySQL BEFORE INSERT triggers are essential tools for maintaining data quality and enforcing business rules at the database level. These triggers automatically execute a series of SQL statements before a new row is inserted into a table, allowing for data validation, modification, and enhancement.An
5 min read
MySQL DROP Trigger
In MySQL, triggers automatically perform actions when events like INSERT, UPDATE, or DELETE occur on a table However, there are situations where a trigger may not be necessary and its logic may need to be updated. In such cases, MySQL provides the DROP TRIGGER statement to delete an existing trigger
4 min read
PL/SQL Enable Triggers
In PL/SQL, triggers are special stored procedures that automatically execute in response to certain events on a particular table or view. These triggers can be used to enforce complex business rules, maintain integrity constraints, or track changes in the database. However, triggers can be enabled o
6 min read
PL/SQL Drop Triggers
In database management, triggers play an important role in maintaining data integrity and enforcing business rules automatically. However, there may be situations where we need to remove an existing trigger from a database. This process is accomplished using the DROP TRIGGER statement in PL/SQL.In t
4 min read
PL/SQL Disable Triggers
Triggers in PL/SQL are designed to automatically perform actions in response to specific events on a table, such as INSERT, UPDATE, or DELETE. However, there are times when you may need to temporarily disable these triggers, To perform updates or to speed up large data imports. In this article, We w
4 min read
MySQL BEFORE UPDATE Trigger
The MySQL triggers are a powerful feature that allows the execute a set of SQL statements automatically in response to certain events on a table. One type of trigger is the BEFORE UPDATE trigger which is invoked before an update operation is performed on the table. This article provides a complete o
4 min read