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:
Explanation: 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:
Explanation: 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:
Explanation: 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.
MariaDB Drop Trigger
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.
Contact Us