SQL 中的级联复制?可以自动复制一条记录及其相关记录吗?

发布于 2024-10-19 11:01:27 字数 326 浏览 2 评论 0原文

您好,我想知道是否存在“级联复制”之类的东西,它在复制记录时尊重外键关系,这样,如果您复制具有指向它的相关记录的记录,它们也会被隐式复制?如果没有,是否有一种优雅的方法可以扩展,这样如果我添加新的 FK 关系或修改我的表,我就不必重新测试代码?

我的情况是,我有一个主数据库和小型移动数据库,我想定期将它们与主数据库合并。假设我有客户 - 订单 - 订单项目,并且在移动数据库中创建了一个带有新订单信息的新客户,我想将其合并到主数据库中,以便整个树都出现。

更新: 我在主数据库和卫星数据库上都使用 SQLite。我只想提交客户订单,而无需手动复制相关订单和订单项目。

任何想法将不胜感激!

Hi I am wondering if there is such thing as a 'cascade copy' that honors foreign key relationships when copying records, so that if you copy a record that has related records pointing to it, they are also implicitly copied? If not is there an elegant way to do this that will scale, so that if I add new FK relationships or modify my tables I dont have to retest code?

My situation is that I have a main database and small mobile databases that I would like to merge with the main one periodically. Lets say I have Customers - Orders - OrderItems and a new Customer is created in the mobile DB with new order information, I would like to merge that into the main DB so the whole tree comes across.

UPDATED:
I am using SQLite on both main and satellite DBs. I would just like to bring across a Customer order without having to manually copy the related Orders and OrderItems.

Any thoughts would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榕城若虚 2024-10-26 11:01:27

没有内置机制可以提供在需要复制时自动复制给定数据库中的行及其相关数据的功能。该逻辑必须封装到存储过程中,在创建新关系时必须更新和测试该存储过程。

但是,如果您正在寻求在两个数据库之间复制数据,则可以通过内置复制机制或使用同步框架之类的工具来实现。

Microsoft 同步框架

There is no built-in mechanism that will provide the functionality of automatically copying a row and its related data in a given database when a copy is desired. That logic must be encapsulated into a stored procedure which would have to be updated and tested when new relationships are created.

However, if what you are seeking is replicating data between two databases, that is possible through the built-in replication mechanism or using something like the Sync Framework.

Microsoft Sync Framework

生寂 2024-10-26 11:01:27

这通常可以通过使用触发器来解决,但这取决于您的数据库平台。或者,您可以考虑存储过程的一般情况。

在我们可以说太多之前,我将阐述您的数据库的性质以及您想要完成的任务的结构。

This would generally be solved with the use of triggers, but it depends on your database platform. Alternately you might consider the general case of stored procedures.

I would expound on the nature of your database and the structure of what you're trying to accomplish before we can say too much more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文