Is it bad to rely on foreign key cascading? 外键 级联操作
Is it bad to rely on foreign key cascading?
I'll preface前言 this by saying that I rarely delete rows period. Generally most data you want to keep. You simply mark it as deleted so it won't be shown to users (ie to them it appears deleted). Of course it depends on the data and for some things (eg shopping cart contents) actually deleting the records when the user empties his or her cart is fine.
I can only assume that the issue here is you may unintentionally delete records you don't actually want to delete. Referential integrity should prevent this however. So I can't really see a reason against this other than the case for being explicit.
When/Why to use Cascading in SQL Server?
Summary of what I've seen so far:
- Some people don't like cascading at all.
Cascade Delete
- Cascade Delete may make sense when the semantics of the relationship can involve an exclusive独有的 "is part of" description. For example, an OrderLine record is part of its parent order, and OrderLines will never be shared between multiple orders. If the Order were to vanish消失, the OrderLine should as well, and a line without an Order would be a problem.
- The canonical权威的 example for Cascade Delete is SomeObject and SomeObjectItems, where it doesn't make any sense for an items record to ever exist without a corresponding main record.
- You should not use Cascade Delete if you are preserving history or using a "soft/logical delete" where you only set a deleted bit column to 1/true.
Cascade Update
- Cascade Update may make sense when you use a real key rather than a surrogate key (identity/autoincrement column) across tables.
- The canonical example for Cascade Update is when you have a mutable foreign key, like a username that can be changed.
- You should not use Cascade Update with keys that are Identity/autoincrement columns.
- Cascade Update is best used in conjunction with a unique constraint.
When To Use Cascading
- You may want to get an extra strong confirmation back from the user before allowing an operation to cascade, but it depends on your application.
- Cascading can get you into trouble if you set up your foreign keys wrong. But you should be okay if you do that right.
- It's not wise to use cascading before you understand it thoroughly. However, it is a useful feature and therefore worth taking the time to understand.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-03-14 pass an instance of class to TestCase as parameter
2018-03-14 How to Integrate .NET Projects with Jenkins
2016-03-14 Knowing When to Use Override and New Keywords (C# Programming Guide)
2016-03-14 Versioning with the Override and New Keywords (C# Programming Guide)
2016-03-14 Polymorphism (C# Programming Guide)
2015-03-14 2-Medium下的MultipleCommandAssembly
2015-03-14 如何获取supersocket的源代码