Definition of MySQL Drop Trigger
MySQL DROP Trigger is a MySQL statement command that is responsible to delete an existing trigger from the database. Like other MySQL Triggers, this DROP trigger is also defined as a stored code program that is invoked when required for a table associated with it. Before we execute the MYSQL DROP trigger the user should gain the administrative privilege to apply the changes for the database table to generate the desired results. We should keep in mind that when any table is dropped from the MySQL database then automatically all the triggers created and linked with the table will be deleted as well.
Syntax:
The MySQL DROP Trigger has the following basic syntax to delete a stored trigger program associated to that specific database table:
DROP TRIGGER [IF EXISTS] [SchemaName.] TriggerName;
From the above structure we come to know that:
- Initially, you need to provide a particular name to the trigger which is required to be removed or dropped after the keywords DROP TRIGGER.
- In the next step, you will have to give the name of the schema where the trigger is stored or created previously. In case if the SchemaName is missing or not mentioned then, the query statement will delete the trigger present in the current MySQL database.
- IF EXISTS is added to the syntax to provide an option provisionally to check if the trigger that we need to drop is available on that particular schema/table or not.
How DROP TRIGGER Statement Work in MySQL?
- There can be various triggers implemented in MySQL Database tables. You need to work for those tables only where the trigger that you want to be dropped is associated with it.
- The trigger was created earlier and for some reason or to avoid the event occurring we can remove the trigger. This action of using DROP Trigger will also not display the logs stored in the trigger-related table. So, we need to be confirmed and sure if we want to delete the trigger or not.
- As per the above syntax, we need schema name and trigger name to be mentioned with the DROP Trigger statement. At the same time, we must include the IF EXISTS option also.
- Suppose if you try to delete a trigger that is not present in the table without applying the option IF EXISTS then, the MySQL server will produce an error. Another is the option IF EXISTS is added to the query then, the server will generate just a NOTE rather than that you can further see through SHOW WARNINGS query command.
- You need to require the administrative level of access to perform the DROP TRIGGER statement for the linked table. Also, dropping the table will result in the removal of all triggers within it.
Examples of MySQL DROP TRIGGER
Let us examine through the following examples to demonstrate more about DROP Trigger and its working & uses in MySQL:
In the beginning, we will first create a table in the database for executing the queries. Let it be named as Rooms where the records are stored and for which a trigger will be created.
The SQL query for creating a table is as follows:
DROP TABLE IF EXISTS Rooms;
CREATE TABLE Rooms ( Room_ID INT PRIMARY KEY AUTO_INCREMENT, RoomName VARCHAR(255) NOT NULL, Flat_Num INT NOT NULL);
Also, let us enter a few records using the INSERT query as:
INSERT INTO Rooms(Room_ID, RoomName, Flat_Num) VALUES('1','pawan Vihar','10');
Querying data rows from Rooms table to see its insides:
SELECT * FROM Rooms;
Output:
Again, forming subsequent table to save the variations that happened on the parental table Rooms after any update command if we are using an AFTER UPDATE Trigger as follows:
CREATE TABLE Roomlogs ( User_ID VARCHAR(255), Info_Update VARCHAR(255));
View the table at present:
SELECT * FROM Roomlogs;
Output:
Now, we are moving towards making the statement to implement an AFTER UPDATE trigger on the Rooms table:
DELIMITER $$
CREATE TRIGGER room_updates
AFTER UPDATE ON Rooms FOR EACH ROW
BEGIN
INSERT into Roomlogs(User_ID,Info_Update) VALUES (user(), CONCAT('Updated Room Info (',OLD.Room_ID,' ',OLD.RoomName,' ',OLD.Flat_Num,') to (',NEW.Room_ID,' ',NEW.RoomName,' ',NEW.Flat_Num,')'));
END$$
DELIMITER ;
After room_updates trigger is generated, this will be frequently triggered before stirring of an update event for every row in the rooms table.
To observe in detail, when you update any value in the Flat_Num column then, a fresh row will be inserted to the roomlogs table to record the changes made.
UPDATE Rooms SET Flat_Num = Flat_Num + 5 WHERE Room_ID <10;
Output:
Again, view to your Roomlogs table and understand the transformation and effect of AFTER UPDATE trigger using the following query:
SELECT * FROM Roomlogs;
Output:
As you can see in the screenshot above that after creating the AFTER UPDATE triggerand making certain modifications in the table rows using UPDATE query, the Roomlogs table has recorded the user id and information about the changes made in the trigger related table Rooms. Suppose, we will create another trigger in the Payment table as follows:
Payment table
Payment_logs table to record the logs after an update query:
Trigger Code:
DELIMITER $$
CREATE TRIGGER `payment_update`
AFTER UPDATE ON `payment` FOR EACH ROW
BEGIN
IF OLD.PAmount <> new.PAmount THEN
INSERT INTO Payment_logs (User_ID,Before_PAmount, After_PAmount)
VALUES (user(), old.PAmount, new.PAmount);
END IF;
END$$
DELIMITER ;
You can even view the trigger present in the Rooms table by going to the Triggers option in the phpMyAdmin server as follows:
SHOW TRIGGERS;
Output:
Finally, we will proceed towards the use of DROP Trigger in MySQL to show the result on table Rooms. Let us delete the second trigger named room_updates using the SQL command below:
DROP TRIGGER room_updates;
Again, view the triggers on table Rooms lastly to validate the removal of the above mentioned trigger.
SHOW TRIGGERS;
Output:
See the trigger has been deleted with its log info from the Rooms table.
Conclusion
- Normally, we create MySQL trigger to produce information to the user for any changes made in the database like MySQL operations: INSERT, DELETE, UPDATE, ALTER statement queries and keep a log of those modifications.
- When we need to remove any trigger available to a specific table then, we will use MySQL DROP Trigger command statement in that database table.
Recommended Articles
This is a guide to MySQL DROP TRIGGER. Here we also discuss the definition and how drop trigger statement work in MySQL? along with different examples and its code implementation. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses