The syntax for creating a database trigger is: [CREATE
< BEFORE | AFTER > ON FOR EACH Row
] . 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<触发器主体>Copy after loginThe 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!
Previous article:Locally connected virtual machine MySQL prompts is not allowed to connect Next article:what are mysql connectorsStatement of this WebsiteThe 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.cnLatest Articles by Author
2024-10-13 13:32:21 2024-10-12 11:58:51 2024-10-11 20:06:51 2024-10-11 18:59:31 2024-10-11 18:30:51 2024-10-11 15:51:51 2024-10-11 15:42:10 2024-10-11 14:41:20 2024-10-11 14:08:31 2024-10-11 13:42:21Latest IssuesRegular expression to match words I have a script where I am trying to match new job names with existing job names in a data...From 2024-04-06 21:24:0401606MySQL gets data from multiple tables I have a eg_design table which contains the following columns: and eg_domains table which ...From 2024-04-06 18:42:4402479Group MySQL results by ID for looping over I have a table with flight data in mysql. I'm writing a php code that will group and displ...From 2024-04-06 17:27:5601406Related TopicsMore>