如何设计一个简单的数据库

时间:2017-07-21 16:32:26

标签: mysql database

我想模仿学生,老师,班级关系。每个学生都与一位老师联系在一起(老师可以有很多学生)。只有三个班级。我想到的方式是有三个表:

学生表 - > (student_id,student_name,class_id)

教师表 - > (student_id,student_name,class_id)

班级表 - > (class_id,class_name)

我不确定如何在表格中显示师生关系。我们如何知道哪位老师被分配到哪位学生?

2 个答案:

答案 0 :(得分:1)

这是比你想要的更多的表,但是Microsoft创建的几个.NET示例都围绕着类似的关系数据库。

以下是该数据库的链接: https://msdn.microsoft.com/en-us/library/bb399731(v=vs.100).aspx

在此示例中,学生和教师都保存在人员表中,并通过两个不同的联接表与课程表相关联。 。学生成绩和课程讲师。

enter image description here

以下是Contoso大学架构的链接: https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

enter image description here

答案 1 :(得分:1)

这可以通过一些简单的连接来完成。

假设您想找到与某位老师相关的所有学生,您可以先抓住teacher的行。然后,您将加入教师教授的classes。最后,您将加入这些类中的students

这被称为多对多关系,是数据库中的一个重要概念。

select
    t.student_name, -- I suspect this col might actually be named teacher_name
    s.student_name, 
from
    -- Find the classes that a teacher teaches
    teacher_table t join class_table c on (t.class_id=c.class_id)
    -- Find the students in those classes
    join student_table s on (s.class_id=c.class_id)
where
    t.student_id = ? -- Again, I suspect this should be "teacher_id"