yariv bar
yariv bar

Reputation: 976

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

Upvotes: 1

Views: 54

Answers (3)

Bhavesh G
Bhavesh G

Reputation: 3028

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

Upvotes: 1

Mihai
Mihai

Reputation: 26784

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.

Upvotes: 3

Dennis van Opstal
Dennis van Opstal

Reputation: 1338

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.

Upvotes: 2

Related Questions