Computer >> Computer tutorials >  >> Programming >> MySQL

Drop trigger if exists in MySQL?


To drop trigger, use DROP command. The syntax is as follows −

DROP TRIGGER IF EXISTS yourTriggerName;

To understand the above syntax, you need to have a trigger in your current database.

To check the trigger is present or not, you can use below query. We have a trigger in our database −

mysql> show triggers;

The following is the output −

+-------------+--------+---------------+------------------------------------------------------------------------+--------+------------------------+--------------------------------------------+---------+----------------------+----------------------+--------------------+
| Trigger     | Event   | Table        | Statement                                                              | Timing |Created                 | sql_mode                                   |  Definer                 | character_set_client | collation_connection | Database Collation |
+-------------+--------+---------------+------------------------------------------------------------------------+--------+------------------------+--------------------------------------------+---------+----------------------+----------------------+--------------------+
| CheckSalary | INSERT | employeetable | if new.EmployeeSalary < 1000 then setnew.EmployeeSalary = 10000;end if | BEFORE | 2018-12-31 17:33:29.54 |STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | root@% | utf8 |utf8_general_ci | utf8mb4_0900_ai_ci |
+-------------+--------+---------------+------------------------------------------------------------------------+--------+------------------------+--------------------------------------------+---------+----------------------+----------------------+--------------------+
1 row in set (0.17 sec)

Here, we have trigger with the name ‘CheckSalary’ on employeetable. Drop the trigger ‘CheckSalary’ using DROP command. The query is as follows −

mysql> drop trigger if exists CheckSalary;
Query OK, 0 rows affected (0.30 sec)

Use the show triggers command to check whether the trigger is present or not. The query is as follows −

mysql> show triggers;
Empty set (0.00 sec)

Look at the above result now, the trigger is not present the database ‘test’. We removed it using drop.