Sobes.tech
Junior — Middle

Is it possible to model and store a family tree structure in a relational database?

sobes.tech AI

Answer from AI

Yes, the family tree structure can be modeled and stored in a relational database. Usually, one of the following models is used:

  1. Hierarchical Model (Adjacency List) — each record contains a reference to its parent:
CREATE TABLE Person (
  Id INT PRIMARY KEY,
  Name VARCHAR(100),
  ParentId INT NULL,
  FOREIGN KEY (ParentId) REFERENCES Person(Id)
);
  1. Nested Sets Model — stores intervals for quick descendant search.

  2. Materialized Path — stores the path from the root to the node as a string.

The hierarchical model is simpler and more commonly used, but recursive queries or additional indexes may be needed for complex tree queries.