简体   繁体   中英

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...). 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...

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.

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).

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.

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.

EDIT:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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