级联删除和更新的优点和缺点是什么?

发布于 2024-09-16 04:06:02 字数 154 浏览 5 评论 0原文

也许这是一个天真的问题......但我认为我们应该始终进行级联删除和更新。但我想知道它是否存在问题以及我们什么时候不应该这样做?我现在真的想不出一种情况,您不想进行级联删除,但我确信有一个......但是更新应该总是进行吗?

那么有人可以列出级联删除和更新的优点和缺点吗?谢谢。

Maybe this is sort of a naive question...but I think that we should always have cascading deletes and updates. But I wanted to know are there problems with it and when should we should not do it? I really can't think of a case right now where you would not want to do an cascade delete but I am sure there is one...but what about updates should they be done always?

So can anyone please list out the pros and cons of cascading deletes and updates ? Thanks.

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

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

发布评论

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

评论(3

超可爱的懒熊 2024-09-23 04:06:02

优点:

  • 当您从父表中删除一行时,所有外键行都将被删除
  • 这通常比使用触发器实现更快 不太
  • 可能出现孤立行

缺点

  • 可能出现孤立行
  • 如果您错误地删除了父表中的一行,则所有行都将被删除相应的子表将被删除,PITA将找出您删除的内容

Pros:

  • When you delete a row from the Parent table all the foreign key rows are deleted
  • This is usually faster than implementing this with triggers
  • Orphaned rows are unlikely

Cons

  • Orphans are possible
  • If by mistake you delete a row in the parent table all the rows in the corresponding child tables will be deleted and it will be PITA to figure out what you deleted
夜还是长夜 2024-09-23 04:06:02

这取决于表中包含的实体:如果没有主键一侧就不能存在外键一侧,则级联删除是有意义的。

例如:如果发票被删除,发票行项目就没有任何权利继续存在。

但是,如果您有一个外键用于员工与其老板之间的关系“为”工作,那么如果老板离开公司,您是否想要删除该员工?

另外:一个技术问题是,如果依赖表条目发生变化,一些 ORM(对象关系映射)工具会感到困惑,而它们对此不负责。

This depends on the entities that are contained in the tables: if the side of the foreign key cannot exist without the side of the primary key, it makes sense to have cascaded delete.

E. g.: An invoice line item does not have any right to survive if the invoice is deleted.

But if you have a foreign key used for the relationship "works for" for the relationship between an employee and his/her boss, would you want to delete the employee if the boss leaves the company?

In addition: a technical issue is that some ORM (object relational mapping) tools are confused if dependent table entries change without them being responsible for that.

梦在深巷 2024-09-23 04:06:02

优点:

  • 数据完整性 - 可以帮助避免记录引用不再存在的内容的情况。

缺点:

  • 性能 - 级联删除/更新可能会很慢。
  • 复杂性——似乎与我一起工作的大多数人都不习惯级联,所以当你给他们一个包含它的新项目时,他们第一次触发这些级联时会感到有点惊讶。
  • 正如其他人已经提到的,如果使用不当,真的会把事情搞砸。

Pros:

  • Data integrity - Can help avoid situations where a record refers to something that is no longer there.

Cons:

  • Performance - Cascading deletes/updates can be sloooooooooooooooooooow.
  • Complexity - It seems most people I work with are not used to cascades, so when you give them a new project that has it, they're a bit surprised the first time they trigger one of these cascades.
  • As others have already mentioned, can really mess things up when used improperly.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文