Mysql triggers (review)

When I was thinking about a deletion record recycle bin today, I suddenly thought of triggers, which were rarely used before. Without further ado, let’s look at its explanation first:

In MySQL, a trigger is a special stored procedure that automatically executes when a specified event (such as inserting, updating, or deleting data) occurs.

Triggers are usually used to implement complex business logic, such as automatically updating related records when new data is inserted, performing some additional cleaning operations when data is deleted, and so on.

There are two types of triggers in MySQL: row-level triggers and statement-level triggers. Row-level triggers are executed when each row of data is affected, while statement-level triggers are executed after each SQL statement is executed.

You will understand after looking at an example:

 

CREATE TRIGGER log_deleted_user_menu_records AFTER UPDATE ON user_menu FOR EACH ROW
BEGIN
       --第一次判断
	IF
		OLD.deletestatus = 'NOT_DELETED' 
		AND NEW.deletestatus = 'USER_DELETED' THEN
			INSERT INTO deleted_records ( table_name, record_id, deleted_at, deletestatus )
		VALUES
			( 'user_menu', OLD.id, NOW(), 'USER_DELETED' );
		-- 第二个判断
		ELSEIF OLD.deletestatus = 'USER_DELETED' 
		AND NEW.deletestatus = 'TRASH_DELETED' THEN
			UPDATE deleted_records 
			SET deleted_at = NOW(),
			deletestatus = 'TRASH_DELETED' 
			WHERE
				table_name = 'user_menu' 
				AND record_id = OLD.id;
                        -- 最后走的判断
			ELSE UPDATE deleted_records 
			SET deleted_at = NOW(),
			deletestatus = 'DELETED' 
			WHERE
				table_name = 'user_menu' 
				AND record_id = OLD.id;
			
		END IF;
		
	END;

log_deleted_user_menu_records trigger name

AFTER UPDATE ON user_menu FOR EACH ROW means to execute after each row of the user_menu table changes (of course it can also be executed before)

OLD.deletestatus refers to the deletestatus value in the user_menu field before the change

NEW.deletestatus corresponds to the new value.

You will know the other sentences at a glance,

Then test it, first look at a record with id=3 in the user_menu table below,

At this time, there is no deleted_records.

Next I want to change the deletestatus value of id=3;

UPDATE test_database.user_menu SET deletestatus = ‘USER_DELETED’ WHERE id = 3

At this time, let's look at the data in the record table; a record is automatically created, which is the role of the trigger;

Then let me tell you about the things to note about triggers.

Here are the pros and cons of triggers and some considerations on how to use them appropriately:

advantage:

  1. Data integrity guarantee: Triggers can enforce business rules and data integrity constraints, such as forcing certain columns to not be empty, prohibiting the insertion of duplicate data, etc.
  2. Automated operations: Triggers can automatically perform operations such as updating the values ​​of other tables or columns when inserting, updating, or deleting data.
  3. Improve database performance: Triggers can reduce application requests to the database, thereby reducing network latency and server load.

shortcoming:

  1. Difficult to maintain: In large databases, triggers can become very complex and difficult to understand and maintain.
  2. Reduced performance: The execution of triggers may reduce database performance, especially when processing large amounts of data.
  3. Security issues: Triggers may be exploited by hackers, compromising the security of the database.

When using triggers appropriately, you need to pay attention to the following points:

  1. Use triggers only when necessary: ​​When designing your database, you should use triggers only when necessary to avoid excessive complexity and performance issues.
  2. Keep it simple: Try to keep the trigger logic simple and easy to maintain.
  3. Testing and debugging: Before using triggers in a production environment, they should be thoroughly tested and debugged to ensure their correctness and stability.
  4. Security: Security issues should be considered when using triggers, such as using appropriate security measures to prevent hackers from using triggers to attack the database.

In short, using triggers can improve data integrity, automate operations, and improve database performance, but you also need to pay attention to its maintenance and performance issues, as well as security issues.

 

Guess you like

Origin blog.csdn.net/wqzbxh/article/details/130261742