Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store family tree data in a mysql database

Tags:

mysql

I have a family tree. I would like to store it in a mysql database. I have a table with a column called "family members," but i don't know how to arrange these family members. For example, I am under my dad and my son is under me. So i guess, how can i store this type of tree in a database?

like image 887
kirby Avatar asked Feb 03 '12 05:02

kirby


People also ask

How can we store tree in database?

The standard method of storing hierarchical data is simple parent-child relationship. Each record in the database includes a —parent id—, and a recursive query through the records build the children, siblings, and levels of the tree.

Which database is best for tree structure?

MongoDB allows various ways to use tree data structures to model large hierarchical or nested data relationships. Presents a data model that organizes documents in a tree-like structure by storing references to "parent" nodes in "child" nodes.

How do you represent a tree in a database?

You could use adjacency lists (your current idea), nested sets, or even (with appropriate database support) arrays of node ids to represent the path from the root. Which representation you choose depends on what you need to do to your data. @Kim, why array? If the graph is a tree, each node has at most one parent.

Can I use MySQL to store files?

A Binary Large Object ( BLOB ) is a MySQL data type that can store binary data such as images, multimedia, and PDF files.


1 Answers

So, you said you have a table with a column called "family members". For me, that's just inappropriate because it doesn't respect normalization :) First of all I would call it "familyTreeId". Now, let's move to the FamilyTree table.

This table would be something like this:

FamilyTree(id, motherId, fatherId, etc) --> etc: if you have additional data

  • id will be the primary key of the table
  • motherId will link to the row in the FamilyTree table that belongs to the mother
  • fatherId will link to the row in the FamilyTree table that belongs to the father

So the rows will be:

+--------+--------------+--------------+
|   id   |   motherId   |   fatherId   |
+--------+--------------+--------------+
| son1   | yourwife     | you          |
| son2   | yourwife     | you          |
| you    | mother       | father       |
| mother | grandmother1 | grandfather1 |
| father | grandmother2 | grandfather2 |
+--------+--------------+--------------+

Other option would be to store the couples

FamilyTreeParents(id, motherId, fatherId)

FamilyTreeNodes(id, familyTreeParentsId)

  • id will be the primary keys of the tables
  • familyTreeParentsId will be a foreign key to a FamilyTreeParents table
  • motherId will be a foreign key to a row in the FamilyTreeNodes table that belongs to the mother
  • fatherId will be a foreign key to a row in the FamilyTreeNodes table that belongs to the father

So the rows will be:

FamilyTreeParents

+----+--------------+--------------+
| id |   motherId   |   fatherId   |
+----+--------------+--------------+
|  1 | yourwife     | you          |
|  2 | mother       | father       |
|  3 | grandmother1 | grandfather1 |
|  4 | grandmother2 | grandfather2 |
+----+--------------+--------------+

FamilyTreeNodes

+--------+---------------------+
|   id   | familyTreeParentsId |
+--------+---------------------+
| son1   |                   1 |
| son2   |                   1 |
| you    |                   2 |
| mother |                   3 |
| father |                   4 |
+--------+---------------------+

Data is more normalized this way because you are not repeating information (like you and yourwife for son1 and son2 as I did in the other solution. However, this solution might be less efficient in terms of speed because there will be needed more joins.

like image 189
Mosty Mostacho Avatar answered Sep 27 '22 15:09

Mosty Mostacho