使用另一个表中的数据创建计算字段

发布于 2024-12-13 11:42:15 字数 511 浏览 0 评论 0原文

我正在为一所学科学校创建数据库设计。我当前的设计显示在下一个链接中:

数据库设计

我附上了三个实体,所有这些都包含计算字段:

  • StudetExams:平均
  • StudentExamObjectives:Accredited 和 Score
  • ObjectiveGrades:FinalScore

但它们需要与另一个表进行交互。例如,StudentExamObjectives 使用分数字段,其中是答案表中所有正确问题的平均值,如果分数值高于或等于 0.70,则认可为 1。

我不知道我的设计是否错误,或者我需要采取一些措施来获取这些值。

提前致谢。

I'm creating a database design for only a subject school. My current design is showed in the next link:

Database design

I've enclosed three entities and all of then contains calculated fields:

  • StudetExams: Average
  • StudentExamObjectives: Accredited and Score
  • ObjectiveGrades: FinalScore

But they need to interact with another tables. For example, StudentExamObjectives use a Score field, where is the avg for all the problems correct in Answers table, then accredited is 1 if Score values is above or equals than 0.70.

I don't know if my design is wrong or I need to do a trick to get those values.

Thanks in advance.

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

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

发布评论

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

评论(2

请你别敷衍 2024-12-20 11:42:15

您遇到的问题是由于缺乏标准化造成的。每当您需要其他表的列值的函数时,您不应该将其存储在表的列中 - 您应该根据需要计算它,作为需要此信息的查询的一部分。您可以使用内联表值函数 (TVF) 来简化此过程,该函数返回具有单个或可能多个列的表。

否则,您必须在插入期间计算和存储结果,从而导致数据冗余,并且如果计算值的任何列发生更改或其行被删除,则可能会出现数据视图不一致的情况。

有时,这种冗余是通过非规范化添加的,但这只能在最坏的情况下完成,即用现实场景分析数据库迫使您朝这个方向发展。如果您采用此方法,请务必添加步骤以确保在任何事务操作后数据库中的完整性。

The problem you are experiencing is due to a lack of normalization. Whenever you have a need for a function of the values of columns of other tables, you shouldn't be storing it in a column in a table - you should be calculating it on demand as part of the query that requires this information. You can simplify this with an inline table-valued function (TVF) returning a table with a single or possibly multiple column(s).

Otherwise, you have to calculate and store the result during inserts, causing data redundancy and the possibility of an incoherent view of your data if any of the columns from which a value is calculated changes or its row is removed.

There are times when such redundancy is added via denormalization, but this should only be done in the worst case, when profiling your database with real world scenarios forces you in this direction. If you go this route, be sure to add steps that make sure that there is integrity in your database after any transactional operation.

ペ泪落弦音 2024-12-20 11:42:15

StudentExamObjectives 使用分数字段,其中是所有考试的平均值
答案表中的问题正确,则如果得分,则认可为 1
值大于或等于 0.70。

我不相信您可以通过这种方式创建计算列。您可以使用带有 CASE 语句的视图吗? (大致)是这样的:

CASE WHEN Score >= 0.70
  THEN 1
  ELSE 0
END AS IsAccredited

Score 的值本身可能是所有考试分数的平均值汇总的结果。很难从您的模型中判断哪些字段当前存在,哪些只是概念性的。

StudentExamObjectives use a Score field, where is the avg for all the
problems correct in Answers table, then accredited is 1 if Score
values is above or equals than 0.70.

I don't believe you can create computed columns in this way. Can you use a view with a CASE statement? Something (roughly) like this:

CASE WHEN Score >= 0.70
  THEN 1
  ELSE 0
END AS IsAccredited

The value for Score may itself be the result of aggregating an average of all exam scores. It's hard to tell from your model which fields currently exist and which are only conceptual.

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