动态表?
我有一个数据库,每门课程有不同的成绩(即课程 1 的三个作业,课程 2 的两个作业,...,课程 N 和 M 作业)。就数据库设计而言,我应该如何处理这个问题?
CourseID HW1 HW2 HW3
1 100 99 100
2 100 75 NULL
编辑 我想我需要重新表述我的问题。截至目前,我有两个表:课程和作业。作业通过外键指向课程。我的问题是我如何知道每堂课有多少作业?
I have a database that has different grades per course (i.e. three homeworks for Course 1, two homeworks for Course 2, ... ,Course N with M homeworks). How should I handle this as far as database design goes?
CourseID HW1 HW2 HW3
1 100 99 100
2 100 75 NULL
EDIT
I guess I need to rephrase my question. As of right now, I have two tables, Course and Homework. Homework points to Course through a foreign key. My question is how do I know how many homeworks will be available for each class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这不是一个好的设计。这是一种反模式,我称之为元数据 Tribbles。您必须不断为每个作业添加新列,并且它们的传播会失去控制。
这是一个重复组的示例,它违反了第一范式关系数据库设计。
相反,您应该为
课程
创建一个表,为作业
创建另一个表。作业中的每一行都引用课程中的父行。您可以为每个作业添加行,然后可以按如下方式对它们进行计数:
当然,这仅计算您用行填充表格之后的作业。所以你(或课程设计者)需要这样做。
No, this is not a good design. It's an antipattern that I called Metadata Tribbles. You have to keep adding new columns for each homework, and they propagate out of control.
It's an example of repeating groups, which violates the First Normal Form of relational database design.
Instead, you should create one table for
Courses
, and another table forHomeworks
. Each row in Homeworks references a parent row in Courses.You'd add rows for each homework, then you can count them as follows:
Of course this only counts the homeworks after you have populated the table with rows. So you (or the course designers) need to do that.
将表分解为三个不同的表。一个保存课程,第二个保存作业,第三个将它们连接起来并存储结果。
课程:
作业:
结果:
Decompose the table into three different tables. One holds the courses, the second holds the homeworks, and the third connects them and stores the result.
Course:
Homework:
Result: