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.

Syntax

The following is the syntax to create a BEFORE DELETE trigger in MySQL:

The BEFORE 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 BEFORE DELETE. This trigger will be invoked before 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 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 queries to define the logic for the trigger. See the below syntax:

Restrictions

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

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.

MySQL BEFORE DELETE Trigger

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.


MySQL BEFORE DELETE Trigger

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.

MySQL BEFORE DELETE Trigger

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:

MySQL BEFORE DELETE Trigger

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:

MySQL BEFORE DELETE Trigger

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.

MySQL BEFORE 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 BEFORE DELETE Trigger

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

MySQL BEFORE DELETE Trigger

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:

MySQL BEFORE DELETE Trigger

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

MySQL BEFORE DELETE Trigger

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

MySQL BEFORE DELETE Trigger

8. If we take at the schema menu, we will see the trigger salaries_before_trigger under the salaries table as follows:

MySQL BEFORE DELETE Trigger




Contact US

Email:[email protected]

MySQL BEFORE DELETE Trigger
10/30