To delete a trigger, use the DROP command. The syntax is as follows −
DROP TRIGGER IF EXISTS yourTriggerName;
In order to understand the above syntax, you need to have a trigger in the current database.
To check if a trigger exists, you can use the following 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 a trigger named 'CheckSalary' on the employeetable. Use the DROP command to remove trigger 'CheckSalary'. 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 exists. The query is as follows −
mysql> show triggers; Empty set (0.00 sec)
Now look at the above results, the trigger does not exist in the database "test". We use drop to delete it.
The above is the detailed content of Delete trigger if it exists in MySQL?. For more information, please follow other related articles on the PHP Chinese website!