Trigger on After Delete with Update Query MySQL Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Node JS CRUD with MySQL Tutorial Example
- How to Create a REST API using Node.js and MySQL?
- How to Export Mysql Database using Command Line in Ubuntu?
- PHP AngularJS MySQL Pagination Example
- Codeigniter 3 - Basic CRUD application with MySQL Example with Demo
- PHP Import Excel File into MySQL Database Tutorial
- How to Import CSV File using MySQL?
- How to fetch this week records in MySql ?
- How to Check Empty or Null Data using MySQL Query?
- How to Copy One Table Data into Another Table using MySQL?
- How to count unique domains from email address field in MySQL ?
- Mysql procedure with pagination in laravel?
- Which MySQL Datatype use for Store an IP Address?
- Mysql Hexadecimal Color Code Store in Binary Datatype Example