Home > Database > Mysql Tutorial > Delete trigger if it exists in MySQL?

Delete trigger if it exists in MySQL?

王林
Release: 2023-09-16 11:57:02
forward
1433 people have browsed it

如果 MySQL 中存在触发器,则删除触发器?

To delete a trigger, use the DROP command. The syntax is as follows −

DROP TRIGGER IF EXISTS yourTriggerName;
Copy after login

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;
Copy after login

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)
Copy after login

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)
Copy after login

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

mysql> show triggers;
Empty set (0.00 sec)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template