Javatpoint Logo
Javatpoint Logo

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.

Syntax

The following is the syntax to create an AFTER DELETE trigger in MySQL:

The AFTER DELETE trigger syntax parameter can be explained as below:

  • First, we will specify the name of the trigger that we want to create. It should be unique within the schema.
  • Second, we will specify the trigger action time, which should be AFTER DELETE. This trigger will be invoked after each row of alterations occurs on the table.
  • Third, we will specify the name of a table to which the trigger is associated. It must be written after the ON keyword. If we did not specify the table name, a trigger would not exist.
  • Finally, we will specify the trigger body that contains a statement for execution when the trigger is activated.

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:

Restrictions

  • We can access the OLD rows but cannot update them in the AFTER DELETE trigger.
  • We cannot access the NEW rows. It is because there are no NEW row exists.
  • We cannot create an AFTER DELETE trigger on a VIEW.

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.

MySQL AFTER DELETE Trigger

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:

MySQL AFTER DELETE Trigger

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.


MySQL AFTER DELETE Trigger

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:


MySQL AFTER DELETE Trigger

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:

MySQL AFTER DELETE Trigger

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:

MySQL AFTER DELETE Trigger

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.

MySQL AFTER DELETE Trigger

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:

MySQL AFTER DELETE Trigger

4. Clicking on the Alter Table option gives the screen as below:

MySQL AFTER DELETE Trigger

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:

MySQL AFTER DELETE Trigger

6. Now, complete the trigger code, review them once again, and if no error is found, click on the Apply button.

MySQL AFTER DELETE Trigger

7. After clicking on the Apply button, click on the Finish button for completion.

MySQL AFTER DELETE Trigger

8. If we look at the schema menu, we can see salaries_AFTER_DELETE trigger under the salaries table as follows:

MySQL AFTER DELETE Trigger




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA