MySQL BEFORE DELETE Trigger
BEFORE DELETE Trigger in MySQL is invoked automatically whenever a delete operation is fired on the table. In this article, we are going to learn how to create a before delete trigger with its syntax and example.
The following is the syntax to create a BEFORE DELETE trigger in MySQL:
The BEFORE 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 queries to define the logic for the trigger. See the below syntax:
BEFORE DELETE Trigger Example
Let us understand how to create a BEFORE 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 salary_archives that keeps the information of deleted salary.
We will then create a BEFORE DELETE trigger that inserts a new record into the salary_archives table before a row is deleted from the salaries table.
In this trigger, we have first specified the trigger name before_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 insert the deleted row into the salary_archives table.
How to call the BEFORE DELETE trigger?
Let us test the above created BEFORE DELETE trigger and how we can call them. So first, we will remove a row from the salaries table:
Second, we will query data from the salary_archives table to verify the above-created trigger is invoked or not by using the select statement:
After executing a statement, we can see that the trigger was invoked successfully and inserted a new record into the salary_archives table.
Third, we will remove all rows from the salaries table:
Finally, we will query data from the salary_archives table again. The trigger was called four times because the DELETE statement removed four records from the salaries table. See the below output:
How to create BEFORE DELETE Trigger in MySQL workbench?
To create a BEFORE DELETE trigger using MySQL workbench, we first need to launch it and then log in using the username and password we created earlier. We will get the screen as follows:
Now do the following steps for creating BEFORE 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. It will show the sub-menu that contains 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 red rectangular box of the previous section, then select the Timing/Event BEFORE 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 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 take at the schema menu, we will see the trigger salaries_before_trigger under the salaries table as follows: