引入 FOREIGN KEY 约束“c_name”在表“t_name”上可能会导致循环或多条级联路径

发布于 2024-12-03 17:57:23 字数 1002 浏览 0 评论 0原文

我有一个名为 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 技术交流群。

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

发布评论

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

评论(2

幻想少年梦 2024-12-10 17:57:23

在任何给定的链接表中,不能有多个级联 RI 链接到单个表。 Microsoft 对此进行了解释:

您收到此错误消息是因为
在SQL Server中,无法出现表
在所有列表中出现多次
级联的参考动作
由 DELETE 或
更新语句。例如,
级联引用动作树
必须只有一条路径
级联上的特定表
参考操作树。

You can't have more than one cascading RI link to a single table in any given linked table. Microsoft explains this:

You receive this error message because
in SQL Server, a table cannot appear
more than one time in a list of all
the cascading referential actions that
are started by either a DELETE or an
UPDATE statement. For example, the
tree of cascading referential actions
must only have one path to a
particular table on the cascading
referential actions tree.

黑色毁心梦 2024-12-10 17:57:23

考虑到 SQL Server 对此的约束,为什么不通过创建一个以 SelectionID (PK)、LessonID、Next_LessonID、QualifyingScore 作为列的表来解决这个问题。使用约束来确保 LessonID 和 QualifyingScore 是唯一的。

在 QualifyingScore 列中,我将使用tinyint,并将其设置为 0、1 或 2。这样,或者您可以执行 QualifyingMinScore 和 QualifyingMaxScore 列,这样您就可以说,

SELECT * FROM NextLesson 
WHERE LessonID = @MyLesson 
AND QualifyingMinScore <= @MyScore 
AND @MyScore <= 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,

SELECT * FROM NextLesson 
WHERE LessonID = @MyLesson 
AND QualifyingMinScore <= @MyScore 
AND @MyScore <= QualifyingMaxScore

Cheers,
Eric

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