引入 FOREIGN KEY 约束“c_name”在表“t_name”上可能会导致循环或多条级联路径
我有一个名为 Lesson
的数据库表:
列: [LessonID、LessonNumber、Description]
...加上一些其他列
我还有另一个名为 Lesson_ScoreBasedSelection
的表:
列: [LessonID,NextLessonID_1,NextLessonID_2,NextLessonID_3]
课程完成后,会在 Lesson_ScoreBasedSelection
表中查找其 LessonID 以获取接下来三个可能的课程,每个课程都与特定的分数范围相关。如果分数为 0-33,则将使用存储在 NextLessonID_1 中的 LessonID。如果分数为 34-66,则将使用存储在 NextLessonID_2 中的 LessonID,依此类推。
我想使用引用课程表中 LessonID 列的外键来约束 Lesson_ScoreBasedSelection
表中的所有列,因为 Lesson_ScoreBasedSelection
表中的每个值都必须在课程表的 LessonID 列。我还希望打开级联更新,这样如果课程表中的 LessonID 发生更改,Lesson_ScoreBasedSelection
表中对它的所有引用都会更新。
这种特定的级联更新似乎是一种非常简单的单向更新,但是当我尝试将外键约束应用于引用 Lesson 表中的 LessonID 字段的 Lesson_ScoreBasedSelection
表中的每个字段时,我得到错误:
在表“Lesson_ScoreBasedSelection
”上引入外键约束“c_name”可能会导致循环或多重级联
任何人都可以解释为什么我会收到此错误,或者如何实现我描述的约束和级联更新?
I have a database table called Lesson
:
columns: [LessonID, LessonNumber, Description]
...plus some other columns
I have another table called Lesson_ScoreBasedSelection
:
columns: [LessonID,NextLessonID_1,NextLessonID_2,NextLessonID_3]
When a lesson is completed, its LessonID is looked up in the Lesson_ScoreBasedSelection
table to get the three possible next lessons, each of which are associated with a particular range of scores. If the score was 0-33, the LessonID stored in NextLessonID_1 would be used. If the score was 34-66, the LessonID stored in NextLessonID_2 would be used, and so on.
I want to constrain all the columns in the Lesson_ScoreBasedSelection
table with foreign keys referencing the LessonID column in the lesson table, since every value in the Lesson_ScoreBasedSelection
table must have an entry in the LessonID column of the Lesson table. I also want cascade updates turned on, so that if a LessonID changes in the Lesson table, all references to it in the Lesson_ScoreBasedSelection
table get updated.
This particular cascade update seems like a very straightforward, one-way update, but when I try to apply a foreign key constraint to each field in the Lesson_ScoreBasedSelection
table referencing the LessonID field in the Lesson table, I get the error:
Introducing FOREIGN KEY constraint 'c_name' on table 'Lesson_ScoreBasedSelection
' may cause cycles or multiple cascade paths.
Can anyone explain why I'm getting this error or how I can achieve the constraints and cascading updating I described?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在任何给定的链接表中,不能有多个级联 RI 链接到单个表。 Microsoft 对此进行了解释:
You can't have more than one cascading RI link to a single table in any given linked table. Microsoft explains this:
考虑到 SQL Server 对此的约束,为什么不通过创建一个以 SelectionID (PK)、LessonID、Next_LessonID、QualifyingScore 作为列的表来解决这个问题。使用约束来确保 LessonID 和 QualifyingScore 是唯一的。
在 QualifyingScore 列中,我将使用tinyint,并将其设置为 0、1 或 2。这样,或者您可以执行 QualifyingMinScore 和 QualifyingMaxScore 列,这样您就可以说,
干杯,
埃里克
Given the SQL Server constraint on this, why don't you solve this problem by creating a table with SelectionID (PK), LessonID, Next_LessonID, QualifyingScore as the columns. Use a constraint to ensure LessonID and QualifyingScore are unique.
In the QualifyingScore column, I'd use a tinyint, and make it 0, 1, or 2. That, or you could do a QualifyingMinScore and QualifyingMaxScore column so you could say,
Cheers,
Eric