Trigger Simple to understand
Trigger (Trigger): Monitors a situation and triggers an action, which is a special stored procedure related to a table event, which is executed either by a program call or by a manual startup, but by an event, such as when working on a table (Insert,delete, Update) is activated when it is executed.
Triggers are often used to enforce data integrity constraints, business rules, and so on. Trigger creation syntax four elements:
-
- Watch location (table)
- Monitoring Events (Insert/update/delete)
- Trigger Time (After/before)
- Trigger Event (Insert/update/delete)
Demand:
Consistency of data between two tables in different databases
Ideas:
Based on the trigger when you operate on table 1, synchronize the operation to table 2
Steps:
Create a trigger that will do the same for table 2 when inserting, updating, and deleting data in table 1
Results:
Table data synchronization between different libraries within the same database connection is fully implemented
Triggers are transactional for two-table operations
Insert-time Trigger
DROP TRIGGER IF EXISTS ' insert_replication_application ';D elimiter;; CREATE TRIGGER INSERT on for Each ROW BEGIN INSERT into VALUES (new value 1, new value 2); END ;;D Elimiter;
Update-time triggers
DROP TRIGGER IF EXISTS' update_replication_application ';D elimiter;;CREATE TRIGGER' Update_replication_application ' afterUPDATE on' Source Library name '. ' Source table name ' forEach ROWBEGINUPDATE' Destination library name '. ' Destination table name 'SET' Destination Field 1 '=New. Modified value 1, ' Destination field 2 '=New. Modified Value 2,WHERE(' Destination primary key field '=Old . SOURCE primary key field);END;;D Elimiter;
Trigger when deleted
DROP TRIGGER IF EXISTS ' delete_replication_application ';D elimiter;; CREATE TRIGGER DELETE on for Each ROW BEGIN DELETE from WHERE (' Destination primary key field '=old. SOURCE primary key field); END ;;D Elimiter;
Did you write a good article? Please sweep the following author's begging code, sponsorship.
Realization of data synchronization between tables based on MySQL trigger