snippetMinor
How to structure a model to properly and efficiently represent tree-like data on relational databases?
Viewed 0 times
efficientlydatabasesrepresentproperlylikerelationalstructurehowanddata
Problem
Based on Traversing tree-like data in a relational database using SQL question, I would like to know how the way regularly used to describe tree-like data on relational databases considering physical implications?
I'm assuming that the RDBMS has not special features to handling that other than regular SQL ANSI or common available features.
In doubt I'm always interested on MySQL and PostgreSQL and eventually SQLite.
I'm assuming that the RDBMS has not special features to handling that other than regular SQL ANSI or common available features.
In doubt I'm always interested on MySQL and PostgreSQL and eventually SQLite.
Solution
I believe he is going for something like a binary tree. I would just include three keys that are tied to the unique id of the same table, one for the left, one for the right child, and one for the parent.
i.e.- (very much pseudocode)
i.e.- (very much pseudocode)
TABLE tree
int id autoinc
varchar(16) data_you_care_about
int parent_id
int left_child_id
int right_child_id
FOREIGN KEY parent_id = tree.id
FOREIGN KEY left_child_id = tree.id
FOREIGN KEY right_child_id = tree.idCode Snippets
TABLE tree
int id autoinc
varchar(16) data_you_care_about
int parent_id
int left_child_id
int right_child_id
FOREIGN KEY parent_id = tree.id
FOREIGN KEY left_child_id = tree.id
FOREIGN KEY right_child_id = tree.idContext
StackExchange Database Administrators Q#62, answer score: 8
Revisions (0)
No revisions yet.