I am trying to create a proper parent/child relationship within my data model. I have a typical one to many relationship between the parent and children.
I am wondering if I have parents that know about their children, is it
- ever acceptable, and
- a good idea
for each child to specifically know about its parent?. (a child can only have one parent in my case)
parent
-------------
PARENT_ID
OTHER_COL
...
child
-------------
CHILD_ID
PARENT_ID // <-- Should this column be here?
OTHER_COL
...
parent_has_children
--------------------
PARENT_ID
CHILD_ID
The advantage I see for having the parent column in the child, is for easily retrieving the parent from a child. But, is this just lazy design?
Thanks in advance.
TL;DR
Re the question:
Yes, and if a foreign key constraint is added from
child.PARENT_IDreferencing the parent columnparent.PARENT_ID, the integrity of the parent-child relationship will be enforced.No, a link or union table like this is used to model a
many-manyrelationship. A many-many relationship between tablesPandCwould imply that the sameCrow can simultaneously associate manyProws, and vice-versa. That is clearly not a parent-child relationship.Modelling the 1-to-many relationship
If the relationship is 1 parent to many children (i.e. the same child can only belong to exactly one parent), then the standard modelling approach is to reference the Parent table from the Child table, via (one of) the Parent’s key columns, usually the Parent’s Primary Key (PK). At the same time, it is also a good idea to Foreign Key (FK) constraint on the reference column (
child.PARENT_ID) to encourage the RDMBS to enforce referential integrity across the relationship:The OP’s additional many:many table
parent_has_childrenis redundant, as it will have exactly one row perchild, and it will soon become a burden to keep this tablein syncwith rows added / removed from the other tables (as failure to keep this synchronized will result in confusion / contradiction in the integrity of the relationship).Re : How do parents know about their children?
Child records for a given parent can be found using a simple query on the child table filtered on the parent foreign key column:
As this is usually a common query, it is always a good idea to ensure that the foreign key
child.PARENT_IDis indexed – some RDBMS versions do this for all foreign keys by default.If you have an entity model (e.g. for an ORM) in an application representing these tables, the parent entity will generally have a collection containing its
childinstances, and on the child entity, the scalar foreign keychild.PARENT_ID‘column’ is either dropped entirely, or replaced with a reference to an instance of the parent: