可空的外键或不同的表

时间:2020-10-08 14:51:37

标签: sql sql-server tsql

我有3个SQL表:

sum

我有第四个表Articles (ArticleId PK) |-- Chapters (ChapterId PK, ArticleId FK) |-- Sections (SectionId PK, ChapterId FK) ,用于存储对EvaluationsArticlesChapters的求值:

Sections

如果要评估某章,则将其Evaluations ( EvaluationId PK, ArticleId NULLABLE FK, ChapterId NULLABLE FK, SectionId NULLABLE FK ) 赋值,并将ChapterIdArticleId留空。

我应该删除这3个FK并改用3个表:SectionIdArticleEvaluationsChapterEvaluations吗?

有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

很多方式,

创建另一个表EvaluationObjectType并将其PK用于评估表。

EvaluationID
EvaluationObjectTypeID (it can be the of one of the three type)
EvaluationObjectID ( chapterid/articleid/sectionid) 

与此有关,您将无法在章/文章/节中明确创建FK。

您应该最终确定设计,以考虑其在查询中(例如在报告和计算中)的用途。

另一种选择是为“章”,“文章”和“节”创建一个组合表。没有看到数据,我不能建议。

相关问题