Let us say I have two different entities on a site.
Teachers and Students.
They both login to the website.Right now I have the following tables
**Users**
- id
- email
- password
**Students**
- GPA
- SAT
- ACT
- user_id references Users ID
**Teachers**
- user_id references Users ID
- classroom_no
- salary
- average_class_size
So when a Student registers I add a row in both the Users and Teacher table.
When a Teacher registers, I add a row in both the Users and Teacher table.
Is it better to just have ONE table Users with the following fields?
**Users:**
- id
- ACT
- GPA
- SAT
- average_class_size
- salary
- classroom_no
- role (0 = teacher 1 = student)
even though some rows would refer to teachers (and thus not use the ACT, SAT, GPA fields)
??
Thanks!
If you are not familiar with database normalization, please read this.
Unless performance is a serious issue, I would not look at using a single table for this structure. As your project goes one, you will more than likely add more fields to each role and possibly add more roles as well. It will become increasingly more difficult to maintain this.
I would actually go a step further and introduce another concept called
roleinto your application, along with the appropriate data model to persist it.Your tables should be:
Having a separate role table will let you separate your authentication table (users) from the rest of the data. This might become important if a user can be both a teacher and a student.
This should give you a basic understanding of how to structure your database.
You may find that you will begin to model some of the other concepts like classes in your system. So adding the appropriate tables may look something like:
Items like the teachers class room now moves to the class, allowing for the teacher to teach multiple classes (maybe you need to re-add the concept of “home room”). The teachers average class size then just becomes a calculation (though you may want to still store it for the sake of efficiency).
Hope this helps guide you in the right direction!