简体   繁体   English

在MySQL中同步同一数据库中两个表的记录

[英]sync records of two tables in the same database in MYSQL

I have two tables with some same fields like: 我有两个具有相同字段的表,例如:

Table A: fname, lname, address, age, email, mobile, website, blog Table B: fname, lname, address, age, email 表A:fname,lname,地址,年龄,电子邮件,手机,网站,博客表B:fname,lname,地址,年龄,电子邮件

Both these tables are used by different modules on my website. 我的网站上的不同模块都使用了这两个表。 I want to sync the first five fields of both tables in such a way that whenever a new row is added or an existing row is modified in Table A, the Table B is updated automatically and vice versa. 我想以这样一种方式同步两个表的前五个字段:每当在表A中添加新行或修改现有行时,表B都会自动更新,反之亦然。

For Example. 例如。

  1. A user created a new record in Table A. Now the Table B should also be updated with this new information. 用户在表A中创建了一条新记录。现在,表B也应使用此新信息进行更新。 and vice versa if a user creates a new record in Table B, the Table A should also be updated with this new information. 反之亦然,如果用户在表B中创建新记录,则表A也应使用此新信息进行更新。

  2. A user modified a record in Table A. Now the Table B should also be updated with this modified information. 用户修改了表A中的一条记录。现在,表B也应使用此修改后的信息进行更新。 and vice versa if a user modifies a new record in Table B, the Table A should also be updated with this modified information. 反之亦然,如果用户修改了表B中的新记录,则还应使用此修改后的信息来更新表A。

How can I achieve this. 我该如何实现。 I thought of using triggers but would it not create an inifinite loop resulting is server error! 我考虑使用触发器,但不会创建无限循环,从而导致服务器错误!

Is any field among those 5 guaranteed to be unique? 这5个字段中是否有任何字段是唯一的? You could add a conditional to the trigger to check to see if that field exists before inserting the record in the table. 您可以在触发器中添加条件,以在将记录插入表中之前检查该字段是否存在。

You might want to rethink the design also. 您可能还需要重新考虑设计。 Storing duplicate records in 2 places seems a little scaring. 在2个地方存储重复记录似乎有些令人恐惧。 You're going to have to have triggers for updates, inserts, and deletes. 您将必须具有更新,插入和删除的触发器。

If u just need to update one table in case the other table gets updated, Instead of creating a table (as a part of some other table), create a View which is also like a table but virtual (not real). 如果您只需要更新一个表以防其他表被更新,则不要创建表(作为其他表的一部分),而是创建一个视图,该视图也类似于表,但是是虚拟的(不是真实的)。
but since u've asked for both sides update. 但自从您要求双方进行更新。

What I believe is that you should go back little back of this problem....and tell us why u need to update both the tables according to the other table,,, 我相信您应该回过头来解决这个问题。...并告诉我们为什么您需要根据另一个表更新两个表,
Because you are just keeping duplicate data at two places that is of no need. 因为您只是在两个位置保留重复数据,所以不需要。

So, try to think whether it can be done without creating two tables, or something like create one table and one view for partial columns requirement. 因此,尝试考虑是否可以在不创建两个表的情况下完成此操作,或者像创建一个表和一个用于部分列需求的视图之类的操作。

It is not an answer to your problem, but I am trying to solve your problem in an optimized way which is good for everyone's health.... Hope you understood what i tried to tell. 这不是您问题的答案,但我正在尝试以一种对每个人的健康都有益的优化方式来解决您的问题。...希望您理解我想说的话。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM