开发者

Database Design - Linking student to teacher

开发者 https://www.devze.com 2023-03-06 03:16 出处:网络
Still working on a web application as a school project, and it just seems to keep expanding. I have to add a layer for teachers so they can manage and coach students through the application. So, I ha

Still working on a web application as a school project, and it just seems to keep expanding.

I have to add a layer for teachers so they can manage and coach students through the application. So, I have to link teachers to their students.

I was wondering what the best way would be to do this. I have one table for all users at the moment. The bare basics of this table are:

id | email | firstname | lastname | role
1  | s@s.s | dromiceio | mimus    | 1
2  | d@d.d | tyranno   | saurus   | 2
3  | a@a.a | utah      | raptor   | 1

Role is the number I assign to them to give them certain permissions. 1 = student, 2 = teacher, 3 = admin.

Assuming that one student has one teacher but one teacher has many students, what would be the best way to design my database table?

I was thinking of creating a new table and just putting the student/teacher ids in it:

For example, if teacher Tyranno Saurus (id 2) has the two students in the table above linked to him, I would make a table like this:

pk_id | teacherid | studentid
  1   |     2     |     1
  2   |     2     |     3

That way, I would know that teacher Tyranno (id 2) has two students, namely the student with userid 1 and userid 3.

Then again, I have never really worked on anything like this so I was wondering if anyone could give me some insight about this and if it's possible to do this in a better way.

I'm building my app in PHP (CodeIgniter) 开发者_如何学编程with MySQL; if that's of importance.

Thanks a lot.


If a student has zero-or-one teacher coaching them, then I would suggest adding a CoachID column to the student table that is a foreign-key to that particular teacher. The intermediate table you've suggested doesn't do anything to simplify this simple relationship, it actually makes it that little bit more complicated.

If you were tying students to classes (where each class has multiple students and each student takes multiple classes) then an intermediate many-to-many mapping table would be a must.

0

精彩评论

暂无评论...
验证码 换一张
取 消