In a database management system(DBMS), triggers are essential for automating actions based on specific events. However, there are times when certain triggers need to be removed or dropped from the database.
In MariaDB, the DROP TRIGGER statement provides a straightforward way to achieve this task. In this article, we'll explore what is DROP triggers and how to use DROP triggers by understanding various examples and so on.
Drop Trigger
The DROP TRIGGER statement in MariaDB removes a trigger from a table in the database. Triggers are database objects that automatically perform actions in response to specified events, such as INSERT, UPDATE, or DELETE operations on a table.
Syntax:
DROP TRIGGER [IF EXISTS] [db_name.]trigger_name;
Explanation: This DROP
TRIGGER
is used to delete a trigger from a table. The optional IF EXISTS
clause prevents an error if the trigger does not exist. The db_name
parameter specifies the database where the trigger is located.
How to Delete a Trigger in MariaDB?
The DROP TRIGGER statement is used to delete unwanted triggers in the table of the database. We may also add the IF EXISTS conditional operator to check if the trigger already exists to prevent potential error if the trigger does not exist.
The database name can also be provided along with the table name. The statement can run only if the user has permission and there are no effects.
Let's set up an Environment to Perform Drop Triggers Operation
To understand How to Delete a Trigger in MariaDB we need a table on which we will perform various operations and queries. Here we will consider a table called customers which contains id, name, email, phone_number, and active as Columns.
CREATE TABLE customers (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
phone_number VARCHAR(20),
active BOOLEAN DEFAULT TRUE
);
Output:
Creation of customers tableExplanation: The query creates a table named as customers with id, name, email, phone_number and active columns with their respective constraints.
Example of Drop Trigger
Let's create an Trigger before performing DROP operations.
The following queries are used to create the triggers for showcasing the example.
Creating Trigger for INSERT Operation
The query creates a trigger named as insert_the_row for the INSERT operation in the customers table.
CREATE TRIGGER insert_the_row
BEFORE INSERT ON customers
FOR EACH ROW
INSERT INTO customers(NAME,email,phone_number,active)
VALUES (NEW.name,NEW.email,NEW.phone_number,1);
-- Use the below query to display the created triggers.
SHOW TRIGGERS;
Output:
Creating trigger for INSERT operationExplanation: The query creates a trigger named as insert_the_row before the INSERT event in the customers table. The statement carried out by the triggers is a INSERT operation to the customers table with the same values that is inserted but with active column hard coded as 1.
Creating Trigger for DELETE Operation
The query creates a trigger named as delete_the_row for the DELETE operation in the customers table.
CREATE TRIGGER delete_the_row
BEFORE DELETE ON customers
FOR EACH ROW
INSERT INTO customers(NAME,email,phone_number,active)
VALUES (OLD.name,OLD.email,OLD.phone_number,0);
-- Use the below query to display the created triggers.
SHOW TRIGGERS;
Output:
Creating trigger for DELETE operationExplanation: The query creates a trigger named as delete_the_row before the DELETE event in the customers table. The statement carried out by the triggers is a INSERT operation to the customers table with the old values of the deleted row but with active column hard coded as 0.
Creating Trigger for UPDATE Operation
The query creates a trigger named as update_the_row for the UPDATE operation in the customers table.
CREATE TRIGGER update_the_row
BEFORE UPDATE ON customers
FOR EACH ROW
INSERT INTO customers(NAME,email,phone_number,active)
VALUES (OLD.name,OLD.email,OLD.phone_number,0);
Output:
Creating trigger for UPDATE operationExplanation: The query creates a trigger named as update_the_row before the UPDATE event in the customers table. The statement carried out by the triggers is a INSERT operation to the customers table with the old values of the updated row but with active column hard coded as 0.
Example of Dropping Triggers
The following queries showcase how to drop a trigger with and without the IF EXISTS conditional operator.
Dropping a Trigger Without IF EXISTS
The following query drops the trigger named as insert_the_row.
DROP TRIGGER insert_the_row;
Output:
Dropping the insert_the_row triggerExplanation: The query deletes the insert_the_row trigger which was created for the INSERT operation in the customers table.
Dropping a Trigger With IF EXISTS
The following query drops all triggers in the customers table.
DROP TRIGGER IF EXISTS update_the_row;
Output:
Dropping the update_the_row triggerExplanation: The query deletes the update_the_row trigger which was created for the UPDATE operation in the customers table.
Conclusion
Triggers are a great tool for automating data management but at times to we may need to drop them. The deletion can be done very easily using the DROP TRIGGER statement but it can process only if the user have adequate permission and there is no cascading effects. This deletion ensure removal of unwanted triggers and ensures data integrity.
Similar Reads
MariaDB Drop View
MariaDB is an open-source relational database management system that is based on SQL(Structured query language). It is an improved version of MySQL and has various features, security, and performance when compared to MySQL. This database is open source with a strong community that can be trusted in
4 min read
MariaDB Create Triggers
Triggers are a very useful and powerful feature of MariaDB. It is a database object associated with a table that activates if an INSERT, UPDATE or DELETE operations are performed. The name itself reflects their action as they run immediately without any human intervention when the respective operati
6 min read
MySQL DROP Trigger
In MySQL, triggers automatically perform actions when events like INSERT, UPDATE, or DELETE occur on a table However, there are situations where a trigger may not be necessary and its logic may need to be updated. In such cases, MySQL provides the DROP TRIGGER statement to delete an existing trigger
4 min read
Drop Table in MariaDB
MariaDB is one of the most widely used open-source relational database management systems, it offers many useful commands for manipulating database structures. It supports the same features that MySQL does but with additional features. Some of the most significant features are new storage engines, J
4 min read
PostgreSQL - DROP TRIGGER
The DROP TRIGGER statement in PostgreSQL is essential for managing database triggers effectively. Triggers in PostgreSQL are database callbacks that automatically execute functions in response to certain events, such as INSERT, UPDATE, or DELETE. DROP TRIGGER provides database administrators and dev
5 min read
PL/SQL Drop Triggers
In database management, triggers play an important role in maintaining data integrity and enforcing business rules automatically. However, there may be situations where we need to remove an existing trigger from a database. This process is accomplished using the DROP TRIGGER statement in PL/SQL. In
5 min read
MariaDB Create View
Views in MariaDB are powerful tools that allow us to create virtual tables based on the result set of a SELECT query. They provide a way to simplify complex queries, improve security by limiting access to certain columns,, and enhance performance by precomputing expensive operations. In this article
5 min read
Quote in MariaDB
Quotes play a crucial role in MariaDB, the open-source relational database management system known for its speed and reliability. Whether you're a beginner or an experienced user, understanding how to use quotes effectively can enhance your database management skills. In this article, we will learn
3 min read
INSERT RETURNING in MariaDB
MariaDB, an open-source relational database management system, continues to evolve with new features and enhancements. One such feature introduced in version 10.5 is the INSERT...RETURNING statement, which offers a convenient and efficient way to insert data into a table and retrieve the inserted ro
3 min read
MariaDB Drop Database
MariaDB, an open-source relational database management system widely used by users, enables them to manage their databases efficiently. MariaDB Offers fast data processing and scalability. MariaDB Stores data in tables with structured relationships between them. In this article, We will learn about
4 min read