我们如何合并具有相同模式的两个数据库? [英] How can we merge two databases with identical schemas?

查看:161
本文介绍了我们如何合并具有相同模式的两个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有两个Rails应用程序实例,每个实例都与自己的数据库对话;我们正在将它们转换为具有单个数据库的单个应用程序.我们已经使它需要特定于特定域的部分正常工作;现在我们只需要合并数据库.我们将数据从一个实例复制到另一个实例的数据库中,并修复ID,以使它们不会重叠.有很多带有很多外键的表.这样做的一种好方法是什么,以使外键仍指向新数据库中的正确行?

We have two instances of a rails application which each talk to their own database; we're in the process of converting them to a single application with a single database. We've already made the parts of it which need to be specific to a particular domain work correctly; now we just need to merge the databases. We're going to copy the data from one instance into the other's database, and fix up the IDs so they don't overlap. There are a lot of tables with a lot of foreign keys. What's a good way of doing this, such that the foreign keys still point to the correct row in the new database?

如果不清楚,我很乐意将糟糕的ascii艺术复杂化.

If this is unclear, I'm happy to complicate matters with bad ascii art.

推荐答案

大多数关系数据库都允许您注释一个外键,以限制其在指向表中的主键更改时进行监视.您可以使用ON UPDATE CASCADE将外键设置为自动更新".对两个数据库中的所有外键都执行此操作,然后更新两个数据库中的所有主键,所有外键都将自动转换.

Most relational databases let you annotate a foreign key to be constrained to watch for when the primary key in the pointed-to table changes. You can set the foreign key to be "auto-updated" when this happens using ON UPDATE CASCADE. Do this for all foreign keys in both databases, then update all primary keys in both databases, and all foreign keys will be automatically converted.

这篇关于我们如何合并具有相同模式的两个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆