Home > Database > Mysql Tutorial > How to create a database trigger

How to create a database trigger

王林
Release: 2020-06-30 16:36:27
Original
6316 people have browsed it

The syntax for creating a database trigger is: [CREATE < BEFORE | AFTER >ON

FOR EACH Row] . Triggers have unique names within the current database.

How to create a database trigger

In MySQL 5.7, triggers can be created using the CREATE TRIGGER statement.

(Recommended learning: mysql tutorial)

The syntax format is as follows:

CREATE <触发器名> < BEFORE | AFTER >
<INSERT | UPDATE | DELETE >
ON <表名> FOR EACH Row<触发器主体>
Copy after login

The syntax description is as follows:

1. Trigger Name

The name of the trigger. The trigger must have a unique name in the current database. If you are creating in a specific database, the name should be preceded by the name of the database.

2. INSERT | UPDATE | DELETE

trigger event, used to specify the type of statement that activates the trigger.

3. BEFORE | AFTER

BEFORE and AFTER, the moment when the trigger is fired, indicate that the trigger is fired before or after the statement that activates it. If you want to verify that the new data meets the conditions, use the BEFORE option; if you want to complete several or more changes after the statement that activates the trigger is executed, you usually use the AFTER option.

4. Table name

The table name associated with the trigger. This table must be a permanent table. Triggers cannot be associated with temporary tables or views. The trigger is activated when a trigger event occurs on the table.

The same table cannot have two triggers with the same trigger time and event. For example, for a data table, you cannot have two BEFORE UPDATE triggers at the same time, but you can have a BEFORE UPDATE trigger and a BEFORE INSERT trigger, or a BEFORE UPDATE trigger and an AFTER UPDATE trigger.

5. Trigger body

The trigger action body contains the MySQL statement that will be executed when the trigger is activated. If you want to execute multiple statements, you can use the BEGIN…END compound statement structure.

6. FOR EACH ROW

Generally refers to row-level triggering, and the trigger action must be activated for each row affected by the triggering event. For example, when using the INSERT statement to insert multiple rows of data into a table, the trigger will execute the corresponding trigger action for each row of data inserted.

The above is the detailed content of How to create a database trigger. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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