级联删除导致多个级联路径 [英] cascading deletes causing multiple cascade paths

查看:114
本文介绍了级联删除导致多个级联路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQlServer 2008,下面显示一些数据表的提取:

I am using SQlServer 2008, and an extract of some datatables is displayed below:

Id(PK)

UserId(PK)
ItemId(PK) 2列的键)
...

UserId (PK) ItemId (PK) - (Compound key of 2 columns) ...

UserId(PK)
ItemId(PK)
VoterId(PK) - (3列的复合键)

UserId (PK) ItemId (PK) VoterId (PK) - (Compound key of 3 columns)

我定义了以下关系:


  • User.Id - > UserItems.UserId

  • (UserItems.UserId,UserItems.ItemId) - >(UserItemVotes.UserId, UserItemVotes.ItemId)

  • UserId.Id - > UserItemVotes.VoterId

现在,我打开级联删除时出现问题。当添加第三关系时,我收到错误...可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
我真的不想这样做,理想情况是如果用户被删除,我想删除他们的用户名和/或他们的投票。

Now, I am having a problem when turning on cascading deletes. When adding the 3rd relationship I receive the error "...may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints." I do not really want to do this, ideally if a user is deleted I would like to remove their useritem and/or their votes.

这是一个坏设计?或者有没有办法从SQL Server获取我想要的行为?

Is this a bad design? Or is there a way to get behaviour I want from SQL Server?

推荐答案

我会导致糟糕的设计。虽然大多数DBMS可以管理级联删除,但使用此内置功能是有风险的。您的场景是为什么这些类型的东西经常在应用程序代码中进行管理的完美示例。在那里,您可以确切地确定需要删除的内容和顺序。

I would lead toward bad design. While most DBMSs can manage cascading deletes, it is risky to use this built in functionality. Your scenario is a perfect example of why these types of things are often managed in application code. There you can determine exactly what needs to be deleted and in what order.

这篇关于级联删除导致多个级联路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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