0

Problem

  1. A student registers for a course.
  2. Add that courses name, and registration date, to the student_courses that can be identified with the student that was registered
  3. Combine the two tables to show the courses the student is registered to.

How should I do this, what is the relationship the Student and student_courses have in order to achieve my goal.

enter image description here

4

3 回答 3

2

我要做的是向 student_courses 添加一个字段作为学生的个人编号,并将 courseId 添加到 student_courses。这样很容易加入所有 3 个表

于 2013-08-08T15:00:23.267 回答
1

您在student_courses表的正确轨道上。

因为学生和课程之间的关系可以是多对多的(即多个学生可以注册多个课程),所以需要一个关系表来将课程链接到学生。

student_courses 应存储对学生和课程主键的外键引用,并包括任何与注册相关的字段(如注册日期)。通过这样做,您可以生成一个 sql 语句,该语句构建学生相关的所有课程的列表:

select
    sc.registrationdate, c.*
from
    student_courses sc
    join student s on s.personalNumber = sc.student_id
    join courses c on c.courseId = sc.courses_id
where 
    s.personalNumber = 123;

或者如果您想查找与课程相关的所有学生:

select
    s.personalNumber, s.firstName, s.lastName
from
    student_courses sc
    join student s on s.personalNumber = sc.student_id
    join courses c on c.courseId = sc.courses_id
where 
    c.courseId = 123;

希望有帮助。

于 2013-08-08T15:04:26.637 回答
0

我在 Courses 和 Student 之间添加了多对多关系,并生成了一个新表。然后我在那个表中添加了一个归档日期。

于 2013-08-30T09:50:06.343 回答