0

I'm trying to improve the database design of my web system. Basically I have some doubts about a table that I have where I store the data of all my users, like FirstName, LastName, etc. This table stores the same data for all my different users, on this case for Students, Profesors and two more type of roles. Right now I have it as one single table call it PERSON. There I have all the relations of my foreign keys, even between "Persons" when a Student have a Profesor as Tutor. But I use some colums only when the Person is a Student, fields Enrolled or PaidUp for example and I keep it as null when is other type of role.

Now, my question is what are the cons and pros to divide the table Person, and create subclasses and store only the foreign key where I need it. Should I keep the single table and use "where" to filter my query, or do I normalize the table in several tables doing a join on Person when I need the general data?

people example

Ellebkey
  • 2,201
  • 3
  • 22
  • 31
  • Possible duplicate of [How can you represent inheritance in a database?](https://stackoverflow.com/questions/3579079/how-can-you-represent-inheritance-in-a-database) – philipxy Jan 26 '18 at 07:02
  • This is a faq, google sql/database subtypes/hierarchies/polymorhism. – philipxy Jan 26 '18 at 07:03

1 Answers1

0

A person is a person and should have its own table. A student or professor is a role played by a person. A person can play many roles. I was a student at my university, then I later taught there.

A student is enrolled in a class, so have an tacit relationship with a professor in that way. You could also track relationships explicitly, as below

Read up on Table Inheritance too

You design should look like this:

PERSON
id
name
dob
...


PERSON_ROLE
person_id references PERSON(id)
type {student, prof, etc}
student_number (nullable)
salary (nullable)
...


PERSON_RELATIONSHIPS
from_person_id references PERSON(id)
to_person_id references PERSON(id)
type {tutorOf, studentOf, ...}
...
Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • So do I only divide the table into 2 tables Person and Person_Role? even if for example salary or student_number stay as null? This system is more focused for elementary and secondary schools, but what you say about student becoming a profesor could be posible for later iterations of the system – Ellebkey Jan 26 '18 at 00:04
  • I disagree. From a database perspective, a student is one kind of thing, and a professor is another. Yes, both are people, and yes it is possible for a person to be both student and professor, but this fact is not relevant to the data model. Keep students and professors separate (says the offspring of a professor and their student ) :-) – Strawberry Jan 26 '18 at 00:11
  • @Strawberry so is okay to have separate tables? How much difference is on doing the query with the join if later I'll have a lot of rows vs having only one table? – Ellebkey Jan 26 '18 at 00:17
  • @Ellebkey Other factors will outweigh this consideration – Strawberry Jan 26 '18 at 07:02