3 ms·
"No, that the relational model is the optimal solution for all data modeling needs." That's so unbelievably incorrect I'm not sure quite where to start. Okay.
by weavejester 15y ago
"No, that the relational model is the optimal solution for all data modeling needs."
That's so unbelievably incorrect I'm not sure quite where to start.
Okay... So let's say we have a tree structure:
A
/ \
B C
/ \ \
D E F
If we want to represent this in a hierarchical or tree database, we do not need to transform the data in in any way.
But if we wanted to represent this as a relation, we'd need to transform the data into a set of tuples. A naive implementation would be:
id value parent
1 A _
2 B 1
3 C 1
4 D 2
5 E 2
6 F 3
Unfortunately this form has a number of problems. For instance, you cannot find all the descendants of A in a single relational algebra query.
So we need a more complex representation, such as the nested set model:
id value lft rgt
1 A 1 12
2 B 2 7
3 C 8 11
4 D 3 4
5 E 5 6
6 F 9 10
In this form, we can find the descendants of A in one query (select value where lft >= 1 and rgt <= 12), but if we wanted to insert a new value before D, we'd have to change the lgt and rgt for all the other rows.
To get around this problem, the numbers used for the lft and rgt values are often large integers. We can't use floats for obvious reasons, and exact decimals tend to be slower than raw integers.
So the best way of representing our tree in a relation looks something like:
id value lft rgt
1 A 1000 12000
2 B 2000 7000
3 C 8000 11000
4 D 3000 4000
5 E 5000 6000
6 F 9000 10000
It still has limitations; you need to worry about rebalancing those integers in exceptional cases, and you need to calculate the lft and rgt values each time you insert a new value. But it's the most optimum way of representing a tree structure in a relational database we know of.
Now, are you going to continue to insist that the relational model is always the best way of representing data?
Are you seriously going to claim the relation above is a better way of representing a tree than, well, a tree?