Realization of data synchronization between tables based on MySQL trigger

Source: Internet
Author: User

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:

      1. Watch location (table)
      2. Monitoring Events (Insert/update/delete)
      3. Trigger Time (After/before)
      4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.