了解更新和删除SSMS 2008中关系的规则 [英] Understanding Update and Delete Rules for Relationships in SSMS 2008

查看:165
本文介绍了了解更新和删除SSMS 2008中关系的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们定义外键约束时,我对SQL Server 2008 Management Studio中更新和删除规则的含义感到困惑。我也没有找到相关的帮助文件(例如F1帮助)。



以下是屏幕快照。感谢有人能描述他们的含义,并推荐一些相关的文件阅读。 : - )




解决方案

外键定义了两个表之间的父子关系。父表中的主键是多达n个子表行中的外键。



现在,如果父表中的主键获取UPDATE,UPDATE RULE踢英寸所有的孩子行也被更新,设置为NULL或其他。最好的做法是有一个永远不会改变的主键(一个固定的ID或一些东西),所以这是不太重要的规则。



更重要的是DELETE规则 - 如果父行被删除(例如,订单被删除)?你也可以用CASCADE DELETE删除所有子行(所有的订单行项),或者你可以设置他们的外键为NULL(他们没有父母了) - 这完全取决于你的具体情况。 >

在订单/订单行场景中,删除完整订单时删除订单行可能非常有用,但您可能不想删除某个产品因为引用它的订单已被删除 - 没有任何一个单一的正确答案 - 这取决于你的场景和你的应用程序。



Marc


I am confused about what means the update and delete rule in SQL Server 2008 Management Studio when we define foreign key constraints. I also did not find related help documents (e.g. F1 help).

Here is the screen snapshot. Appreciate if anyone could describe what do they mean and recommend some related documents to read. :-)

解决方案

The foreign key defines a parent - child relationship between two tables. The primary key in the parent table is the foreign key in the up to n child table rows.

Now if that primary key in the parent table gets UPDATE, the UPDATE RULE kicks in. Either all the child rows are also updated, set to NULL or whatever. Best practice however is to have a primary key that NEVER changes (a fixed ID or something), so that's the less important rule.

The more important one is the DELETE rule - what if the parent row is deleted (e.g. the Order is deleted)? You can either also delete all child rows (all the Order line items) with CASCADE DELETE, or you can set their foreign key to NULL (they don't have a parent anymore) - that's totally up to your concrete scenario.

In the Order/order lines scenario, it might be totally useful to delete the order lines when the complete order gets deleted, but you probably don't want to delete a product, just because an order that references it has been deleted - there's no one single CORRECT answer - it depends on your scenario and your app.

Marc

这篇关于了解更新和删除SSMS 2008中关系的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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