[英]1-M Relationship database design
I'm trying to come up with a database design for the following scenario. 我正在尝试针对以下情况设计数据库。
Student can register to a Programme, at a given time student can have only one registered programme.However, he/she must be able to change the registered programme at any given time (including registering to a new programme).
学生可以注册某个程序,在指定的时间只能注册一个程序,但是他/她必须能够在任何给定时间更改注册的程序(包括注册一个新程序)。 Ultimately, student can be registered to multiple programme but he must have only 1 active programme.
最终,学生可以注册到多个程序,但他必须只有一个有效程序。
I think it should be a 1-M relationship but how to handle this "1 active programme at a given time" situation? 我认为应该是1-M关系,但是如何处理“给定时间有1个活动程序”的情况?
Your student table will have the ProgramID
in relation to the Program
table for example that he/she chooses and would be the current program. 您的学生表将具有与
Program
表相关的ProgramID
,例如,他/她选择的表将是当前程序。 Now, every time he/she change his/her program that ProgramID
will change however there will be a ProgramHistory
to record the changes. 现在,每次他/她更改程序时,
ProgramID
都会更改,但是会有一个ProgramHistory
记录更改。
So possible table would be Student
, Program
, ProgramHistory
. 因此可能的表将是
Student
, Program
, ProgramHistory
。
Example : 范例 :
Student 学生
StudentID Lastname Firstname Gender ProgramID
------------------------------------------------------
101 Smith Jason M 1
102 Jones Kate F 2
Program 程序
ProgramID ProgramName
------------------------------
1 Computer Science
2 Nursing
3 Electrical Engineering
ProgramHistory 程序历史
ID ProgramID StudentID Semester Year
-----------------------------------------------------
1 3 101 Spring 2014
2 2 102 Fall 2014
3 1 101 Fall 2014
To allow for tracking of the history of program enrollment, you need to have a ProgramHistory
table that is the intersection of a many-to-many relationship between Student
and Program
为了跟踪程序注册的历史记录,您需要有一个
ProgramHistory
表,该表是Student
和Program
之间多对多关系的交集
There are a couple of ways to ensure that there is only one active program at one time for a given student. 有几种方法可以确保给定的学生一次只拥有一个活动程序。
One way would be to put an active_program_key
column in your student table and make it a foreign key to the Program
table. 一种方法是在您的学生表中放置一个
active_program_key
列,并使其成为Program
表的外键。 This is probably not the best alternative, since it requires denormalizing data and the resulting duplication might result in data inconsistencies unless you take significant steps to avoid them. 这可能不是最佳选择,因为这需要对数据进行非规范化,并且所导致的重复操作可能会导致数据不一致,除非您采取重要措施避免它们不一致。
Another option using declarative constraints is to create a unique index on the ProgramHistory
table that includes the student_key
and the enrollment_date
. 使用声明性约束的另一种方法是在
ProgramHistory
表上创建一个唯一索引,其中包括student_key
和enrollment_date
。 This ensures that a student can only enroll in once per given date. 这样可以确保每个给定日期学生只能注册一次。 The active program will be the record with the latest date for any given student.
活动程序将是任何给定学生的最新日期的记录。
This second option is simple and avoids duplicating any data. 第二个选项很简单,避免了重复数据。 In fairness, the query to retrieve current student enrollments will be slightly more complicated.
公平地说,检索当前学生入学的查询会稍微复杂一些。 As always, design is about trade-offs.
与往常一样,设计是权衡的。
Assuming that students can change programs at just about any time (ie not just between semesters) then you want to have a program_start_date
in your ProgramHistory
table. 假设学生可以在几乎任何时间(即,不仅在学期之间)更改程序,那么您希望在
ProgramHistory
表中有一个program_start_date
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.