针对同一目标表/列创建两个级联外键

发布于 2024-08-27 13:05:19 字数 589 浏览 4 评论 0原文

我有以下表格:

 user (userid int [pk], name varchar(50))
 action (actionid int [pk], description nvarchar(50))

被捕获关系的另一个表引用:

<user1> <action>'s <user2>.

我使用下表执行此操作:

userAction (userActionId int [pk], actionid int [fk: action.actionid], **userId1 int [fk ref's user.userid; on del/update cascade],  userId2 int [fk ref's user.userid; on del/update cascade]**).

但是,当我尝试保存 userAction 表时,我收到错误,因为我有两个针对 user.userid 的级联 fk。

有什么办法可以解决这个问题还是我必须使用触发器?

I have the following tables:

 user (userid int [pk], name varchar(50))
 action (actionid int [pk], description nvarchar(50))

being referenced by another table that captures the relationship:

<user1> <action>'s <user2>.

I did this with the following table:

userAction (userActionId int [pk], actionid int [fk: action.actionid], **userId1 int [fk ref's user.userid; on del/update cascade],  userId2 int [fk ref's user.userid; on del/update cascade]**).

However, when I try to save the userAction table i get an error because I have two cascading fk's against user.userid.

Is there any way to remedy this or must I use a trigger?

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

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

发布评论

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

评论(1

她比我温柔 2024-09-03 13:05:19

您可以使用触发器,但我认为最好实际编写删除操作。如果删除了大量记录,而我们不允许这样做,那么级联删除通常会导致性能不佳。您不能根据应用程序一次仅删除一条记录的事实来使用它们。如果该一条记录有数千条子记录怎么办?万一有几百万呢?级联删除或者通过触发器删除其他表是极其危险的行为。最好按照您想要的方式编写删除代码,如果子记录太多,甚至可以进行批处理。此外,存在子记录这一事实通常是您不应删除父记录的原因。例如,假设客户端 A 不再是活动客户端。但他过去从你那里买过价值数百万美元的东西。您真的想通过删除一百万个旧订单来搞乱会计记录吗?也许将记录标记为非活动是更好的选择。

You could use a trigger, but I think it is better to actually write the deletes. Cascading deletes are often the casue of poor performance if a large set of records gets deleted and we do not allow them here. You can't base their use on the fact that the application only deletes one record at a time. What if that one record has thousands of child records? What if it has millions? Cascaded delete or deleting other tables through a trigger is extremely risky behavior. It is better to write the code the delete exactly what you want, perhaps even doing batch processing if there are too many child records. Further the fact that there are child records is often a reson why you shouldn't delete the parent record. For instance suppose client A is no longer an active client. But he has bought millions of dollars of stuff from you in the past. Do you really want to mess up the accounting records by deleting a million old orders? Maybe marking the record as inactive is a better choice.

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