How to Delete a Trigger in MySQL

08/06/2021

Contents

In this article, you will learn how to delete a trigger in MySQL.

Deleting a trigger in MySQL

In MySQL, a trigger is a database object that is associated with a table and automatically executes in response to certain events, such as INSERT, UPDATE, or DELETE operations. If you no longer need a trigger, you can delete it using the DROP TRIGGER statement.

Syntax

The basic syntax of deleting a trigger in MySQL is as follows:

DROP TRIGGER [IF EXISTS] trigger_name;

The DROP TRIGGER statement removes the trigger from the database. The IF EXISTS clause is optional and prevents an error from occurring if the trigger does not exist.

Examples

Deleting a trigger that audits INSERT operations

In this example, we will delete a trigger that audits INSERT operations on a table.

DROP TRIGGER IF EXISTS audit_insert;

This statement removes the trigger named audit_insert from the database. The IF EXISTS clause prevents an error from occurring if the trigger does not exist.

Deleting a trigger that enforces business rules

In this example, we will delete a trigger that enforces a business rule by preventing a row from being inserted if a certain condition is not met.

DROP TRIGGER IF EXISTS check_salary;

This statement removes the trigger named check_salary from the database. The IF EXISTS clause prevents an error from occurring if the trigger does not exist.

Deleting a trigger that maintains referential integrity

In this example, we will delete a trigger that maintains referential integrity by deleting all child rows when a parent row is deleted.

DROP TRIGGER IF EXISTS delete_child;

This statement removes the trigger named delete_child from the database. The IF EXISTS clause prevents an error from occurring if the trigger does not exist.