Triggers can cause other SQL code to run before or after a statement is executed. Triggers can read what data was changed by the triggering statement, but there is no return value. Therefore, you can use triggers to enforce business logic constraints without writing corresponding code in the application. From the above description, we can see that triggers can simplify the logic of the application and improve performance. This is because using triggers reduces the number of interactions between the application and the server. Also, triggers help to automatically update normalization and statistics. For example, we can use triggers to automatically count the total transaction amount, number of orders, and average order value. However, the application scenarios of MySQL triggers are also very limited. If you have used triggers from other database products, do not assume that MySQL can achieve the same functions. For example:
CREATE TRIGGER trigger name BEFORE | AFTER trigger event ON table name FOR EACH ROW BEGIN Execute statement list; END The execution statement list supports single or multiple statements. The following is an example of multiple statements: DELIMITER $$ CREATE TRIGGER user_create_log AFTER INSERT ON t_users FOR EACH ROW BEGIN DECLARE log_info VARCHAR(40) character set utf8; DECLARE description VARCHAR(20) character set utf8;#Later, it was found that the Chinese character encoding was garbled, so the character set was set here SET description = " is created"; SET log_info = CONCAT(NEW.user_name, description); #CONCAT function can concatenate strings INSERT INTO logs(log) values(log_info); END $$ DELIMITER ;
The biggest limitation among most is the design of FOR EACH ROW, which sometimes prevents triggers from being used to maintain statistics and cache tables, as this can be slow. The main reason for using triggers is that they can consistently maintain data consistency compared to scheduled synchronous updates. Triggers also cannot guarantee atomicity. For example, a trigger that updates a MyISAM table cannot be rolled back if an error occurs in the source SQL statement. Furthermore, the triggers themselves may be faulty. If we use AFTER UPDATE based on the MyISAM data table to update another table. If the trigger has an error that causes the operation on the second table to fail, the operation on the first table will not be rolled back. InnoDB trigger-related operations, including source statements, are all in the same transaction and therefore meet atomicity requirements. However, if you use InnoDB triggers to verify data consistency with another table, this may lead to incorrect results if you are not careful. For example, if you need to use triggers to simulate foreign keys, you can use a BEFORE INSERT trigger to verify whether a corresponding record exists in another table. However, if you do not use SELECT FOR UPDATE when the trigger reads data from another table, incorrect results may occur due to concurrency issues. Although the trigger has some flaws, it doesn't mean it can't be used. Conversely, triggers can also be useful in their own right, especially for constraints, system maintenance tasks, and keeping statistics up to date. You can also use triggers to record changes in data rows. In this way, even records of offline manual database operations (such as repairing incorrect data) can be recorded. However, it is important to be careful when inserting data into other auto-increment primary key tables. This may cause problems for replicated statements because the auto-increment values are different for two identical replicas. Conclusion: Triggers can play their advantages in limited situations, such as statistical data, data table change logs, etc. However, there are also some defects. For example, the update of large amounts of data will reduce efficiency due to row-by-row triggering. Also, the MyISAM engine cannot guarantee atomicity. Therefore, it depends on the application scenario whether there is a trigger. The above is the detailed content of the use of MySQL triggers. For more information about MySQL triggers, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: JavaScript Basics Operators
Table of contents Tutorial Series 1. Install Mari...
Table of contents Three steps to operate the data...
When it comes to databases, one of the most frequ...
Recent requirements: Global loading, all interfac...
MySQL 8.0 service cannot be started Recently enco...
Preface Last week, a colleague asked me: "Br...
1. Background of the incident: Because of work ne...
When browser vendors bend the standards and take i...
question I encountered a problem when writing dat...
From the backend to the front end, what a tragedy....
This article shares the specific code of the vue3...
Table of contents Preface Promise chaining MDN Er...
This article shares the specific code of JavaScri...
Pitfalls encountered during project deployment Wh...
Preface In web applications, in order to save tra...