Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grounds for having a one to one relationship between tables

If I have two different types of user, Parent and Child. They have identical fields, however a Child has a one to many relationship with exams, a relationship that does not exist for Parents.

Would Parent and Child best be modelled as a single table, or combined?

What if I have two different types of user, Parent and Child. They are the same apart from a child belongs to a school (a school has many children)

again, Would Parent and Child best be modelled as a single table, or combined?

like image 481
pingu Avatar asked Jan 25 '26 06:01

pingu


1 Answers

They have identical fields, however a Child has a one to many relationship with exams

Even when fields are the same, different constraints1 means you are dealing with logically separate entities. Absent other factors, separate entities should be put into separate physical tables.

There may, however, be reasons to the contrary. For example, if there is a key that needs to be unique across parents and children combined, or there is another table that needs to reference all of them etc...

If that's the case, then logically both "parent" and "child" are inheriting from the "person", containing the common constraints (and fields). Such "inheritance" can be represented by either storing the whole hierarchy into a single table (and setting unused "portion" to NULL), or by separating all three "classes" into their own tables, and referencing the "base class" from "inherited classes", for example2:

enter image description here

PERSON_ID is unique across all parents and children. In addition to that, OTHER_TABLE can reference it directly, instead of having to separately reference PARENT_ID or CHILD_ID.


1 A foreign key in this case.

2 A very simplified model that just illustrates the point above and does not try to model everything you mentioned in your question

like image 133
Branko Dimitrijevic Avatar answered Jan 28 '26 02:01

Branko Dimitrijevic