Trigger on After Delete with Update Query MySQL Example

By Hardik Savani November 5, 2023 Category : PHP MySql SQL

Hi Guys,

Now, let's see a tutorial of trigger on delete with update mysql example. We will look at an example of trigger after delete mysql example. I’m going to show you about mysql after delete trigger old. I explained simply about mysql trigger delete update query. Follow the below tutorial step of example mysql trigger after delete.

In MySQL, you can create a trigger that executes an update query after a row is deleted from a table. Here's an example:

Suppose we have a table named employees with columns id, name, and is_active. We want to update the is_active column of the corresponding row in a separate employee_log table after an employee is deleted. We can create a trigger as follows:

Example:

CREATE TRIGGER after_employee_delete

AFTER DELETE ON employees

FOR EACH ROW

BEGIN

UPDATE employee_log SET is_active = 0 WHERE id = OLD.id;

END;

In this example, we create a trigger named after_employee_delete that is executed after a row is deleted from the employees table. The FOR EACH ROW clause specifies that the trigger is executed once for each row that is deleted.

In the body of the trigger, we execute an update query that sets the is_active column to 0 in the employee_log table for the corresponding id. We use the OLD.id syntax to refer to the id column of the deleted row.

Note that the employee_log table must already exist with the appropriate columns for this trigger to work. Also, make sure to test the trigger thoroughly before using it in a production environment.

I hope it can help you...

Tags :
Shares