Home >Database >Mysql Tutorial >Does mysql have triggers?

Does mysql have triggers?

青灯夜游
青灯夜游Original
2022-06-21 14:27:273894browse

mysql has triggers. MySQL trigger is a set of SQL statements stored in the database directory. It is a powerful tool for managing data in MySQL and is mainly used to protect the data in the table. MySQL supports three triggers: 1. INSERT trigger, which is a trigger that responds before or after the INSERT statement is executed; 2. UPDATE trigger, which is a trigger that responds before or after the UPDATE statement is executed; 3. DELETE trigger , is a trigger that responds before or after the DELETE statement is executed.

Does mysql have triggers?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

mysql has triggers.

MySQL triggers are the same as stored procedures. They are a program embedded in MySQL. They are a set of SQL statements stored in the database directory and are a powerful tool for managing data in MySQL. The difference is that the execution of a stored procedure requires a CALL statement, while the execution of a trigger does not require a CALL statement or manual startup. Instead, it is triggered and activated through related operations on the data table to achieve execution. For example, its execution will be activated when an operation (INSERT, DELETE or UPDATE) is performed on the student table.

Triggers are closely related to data tables and are mainly used to protect the data in the tables. Especially when there are multiple tables that are related to each other, triggers can maintain data consistency in different tables.

In MySQL, triggers can only be activated when performing INSERT, UPDATE, and DELETE operations. Other SQL statements will not activate triggers.

Advantages and disadvantages of triggers

The advantages of triggers are as follows:

  • The execution of triggers is automatic. Execute immediately after making corresponding modifications to the data in the trigger-related table.

  • Triggers can implement more complex checks and operations than FOREIGN KEY constraints and CHECK constraints.

  • Triggers can implement cascading changes to table data, ensuring data integrity to a certain extent.

The disadvantages of triggers are as follows:

  • The business logic implemented using triggers is difficult to locate when a problem occurs, especially when it comes to The presence of multiple triggers will make subsequent maintenance difficult.

  • Using a large number of triggers can easily cause the code structure to be disrupted and increase the complexity of the program.

  • If the amount of data that needs to be changed is large, When large, the execution efficiency of the trigger will be very inefficient.

There are three types of MySQL triggers:

MySQL supports three types of triggers: INSERT trigger, UPDATE trigger and DELETE trigger. .

1) INSERT trigger

A trigger that responds before or after the INSERT statement is executed.

2) UPDATE trigger

A trigger that responds before or after the UPDATE statement is executed.

3) DELETE trigger

A trigger that responds before or after the DELETE statement is executed.

[Related recommendations: mysql video tutorial]

The above is the detailed content of Does mysql have triggers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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