简体   繁体   English

MySQL数据库结构的良好实践

[英]good practice in MySQL DB structure

I'm working on a system to manage students attendance in class. 我正在开发一个用于管理班级学生出勤率的系统。 I created two tables: student (student_id,name,mail..) and course (course_id, name, lecturer...). 我创建了两个表:学生(学生ID,名称,邮件..)和课程(课程ID,名称,讲师...)。 now my question is how should i keep record of which students taking which courses? 现在我的问题是我应该如何记录哪些学生修读哪些课程?

should i create another table for each course with this structure: course_id,lecturer,student_1,students_2,student_3... 我是否应该为每个具有以下结构的课程创建另一个表:course_id,lecturer,student_1,students_2,student_3 ...

or maybe there is a better solution for creating this relation? 也许有更好的解决方案来创建这种关系? tx 发射

UPDATE: i should have mentioned that student can take several courses 更新:我应该提到学生可以参加几门课程

Since there is a many to many relations between your tables(every student can take many courses,each course can be taken by multiple students) you need an intermediary table which hold the primary key of both table. 由于您的表之间存在多对多的关系(每个学生可以参加许多课程,每个课程可以由多名学生参加),因此您需要一个中间表,该表具有两个表的主键。

coursestudent(course_id,student_id)

with FOREIGN KEYs to the respective tables. 与FOREIGN KEYs到各个表。

Depends, if a student can have multiple courses and a course belongs to multilple students you want to make a table that contains id, course_id(FOREIGN KEY) and student_id(FOREIGN KEY). 取决于,如果一个学生可以有多门课程,并且一门课程属于多名学生,则您要制作一个包含id,course_id(FOREIGN KEY)和student_id(FOREIGN KEY)的表。

If a student can have only one course, but a course can be followed by multiple students you probably want to add course_id to student as foreign key. 如果一个学生只能学习一门课程,但是一门课程后可以有多个学生,则您可能希望将course_id作为外键添加到该学生。

You need two tables, 你需要两个桌子

students (student_id, studentName, student.....)

courses (course_id, student_id, courseName, course....)

Here, student is related to course(s) by student_id along with course_id in courses table. 在这里,学生通过student_idcourses表中的course_idcourses

EDIT: 编辑:

course_id   student_id      courseName
    c12         s34             DB
    c12         s35             DB
    c43         s86             OS
    c65         s45             PHP
    c57         s86             OS
    ...         ...             ...
    ...         ...             ...
    ...         ...             ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM