[英]How to sync two tables that are not identical?
I have two projects using the same data. 我有两个使用相同数据的项目。 However, this data is saved in 2 different databases.
但是,此数据保存在2个不同的数据库中。 Each of these two databases has a table that is almost the same as his counterpart in the other database.
这两个数据库中的每个数据库都有一个与另一个数据库中的对应表几乎相同的表。
I am looking for a method to synchronise two tables. 我正在寻找一种同步两个表的方法。 Easier said, if
database_one.table
gets an insert, that same record needs to be inserted into database2.table
. 更简单地说,如果
database_one.table
获得插入,则需要将同一记录插入database2.table
。
Table Products
| product_id | name | description | price | vat | flags |
Table Articles
| articleId | name_short | name | price | price_vat | extra_info | flags |
I have never used and wouldn't know how to use any method of database synchronisation. 我从未使用过,也不知道如何使用任何数据库同步方法。 What also worries me is that the tables are not identical and so I will somehow need to map columns to one another.
还让我担心的是,表并不相同,因此我将需要以某种方式将列相互映射。
For example: 例如:
database_one.Products.name -> database_two.articles.name_short
Can someone help me with this? 有人可以帮我弄这个吗?
You can use MERGE function: https://www.mssqltips.com/sqlservertip/1704/using-merge-in-sql-server-to-insert-update-and-delete-at-the-same-time/ 您可以使用MERGE函数: https : //www.mssqltips.com/sqlservertip/1704/using-merge-in-sql-server-to-insert-update-and-delete-at-the-same-time/
Then create a procedure that runs at desired frequency or if it needs to be instant insert the merge into a trigger. 然后创建一个以所需频率运行的过程,或者如果需要立即运行,则将合并插入触发器。
One of possible method is to use triggers. 一种可能的方法是使用触发器。 You need to create trigger for insert, update and delete on database_one.table, that does coresponding operation on database2.table.
您需要在database_one.table上创建用于插入,更新和删除的触发器,该触发器在database2.table上执行核心响应操作。 I guess, that there won't be any problems with insert/update/delete between both databases.
我想,两个数据库之间的插入/更新/删除不会有任何问题。 When using triggers, you can very easily map columns.
使用触发器时,可以非常轻松地映射列。 However you need to consider prons and cons of using triggers - read something here or here .
但是,您需要考虑使用触发器的优缺点- 在此处或此处阅读某些内容。 From my experience performance is very important, so if you have heavy loaded DB it is not a good idea to use triggers for data replication.
根据我的经验,性能非常重要,因此,如果数据库负载很重,那么使用触发器进行数据复制不是一个好主意。 Maybe you should check this too?
也许您也应该检查一下 ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.