The syntax for creating a database trigger is: [CREATE da886c9bf004997d18c9dbb4652dad8f ca226cb945ac95856ecfac65bf7545e4
ON ccc43248daffbac9770dee47fdaff697 FOR EACH Rowac935826f2255710851714eb82edbaf2] . Triggers have unique names within the current database.
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<触发器主体>
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!