MySQL AFTER DELETE Trigger
The AFTER DELETE Trigger in MySQL is invoked automatically whenever a delete event is fired on the table. In this article, we are going to learn how to create an AFTER DELETE trigger with its syntax and example.
The following is the syntax to create an AFTER DELETE trigger in MySQL:
The AFTER DELETE trigger syntax parameter can be explained as below:
If we want to execute multiple statements, we will use the BEGIN END block that contains a set of SQL queries to define the logic for the trigger. See the below syntax:
AFTER DELETE Trigger Example
Let us understand how to create an AFTER DELETE trigger using the CREATE TRIGGER statement in MySQL with an example.
Suppose we have created a table named salaries to store the salary information of an employee as follows:
Next, we will insert some records into this table using the below statement:
Execute the SELECT query to see the table data.
Third, we will create another table named total_salary_budget that keeps the salary information from the salaries table.
Fourth, we will use the SUM() function that returns the total salary from the salaries table and keep this information in the total_salary_budget table:
Execute the SELECT statement to verify the table:
We will then create an AFTER DELETE trigger that updates the total salary into the total_salary_budget table after a row is deleted from the salaries table.
In this trigger, we have first specified the trigger name after_delete_salaries. Then, specify the triggering event. Third, we have specified the table name on which the trigger is associated. Finally, we have written the trigger logic inside the trigger body that updates the total salary into the total_salary_budget table after a row is deleted from the salaries table.
How to call the AFTER DELETE trigger?
First, we will delete a salary from the salaries table using the following statements to invoke the above-created trigger:
Next, we will query data from the total_salary_budget table. We can see that table has been modified after the execution of the query. See the below output:
In the output, we can see that the deleted salary reduces the total_budget.
Third, we will remove all data from the salaries table:
Again, we will query data from the total_salary_budget table. We can see that the trigger updated the table to zero after the execution of the query. See the below output:
How to create AFTER DELETE Trigger in MySQL workbench?
To create an after insert trigger using this tool, we first need to launch the MySQL Workbench and log in using the username and password we have created earlier. We will get the screen as follows:
Now do the following steps for creating an AFTER DELETE trigger:
1. Go to the Navigation tab and click on the Schema menu that contains all the databases available in the MySQL server.
2. Select the database (for example, employeedb), double click on it that shows the sub-menu containing Tables, Views, Functions, and Stored Procedures. See the below screen.
3. Expand the Tables sub-menu and select the table on which you want to create a trigger. After selecting a table, right-click on the selected table (for example, salaries), and then click on the Alter Table option. See the below image:
4. Clicking on the Alter Table option gives the screen as below:
5. Now, click on the Trigger tab shown in the previous section's red rectangular box, then select the Timing/Event AFTER DELETE. We will notice that there is a (+) icon button to add a trigger. Clicking on that button, we will get a default code on the trigger based on choosing Timing/Event:
6. Now, complete the trigger code, review them once again, and if no error is found, click on the Apply button.
7. After clicking on the Apply button, click on the Finish button for completion.
8. If we look at the schema menu, we can see salaries_AFTER_DELETE trigger under the salaries table as follows: