I have a question on best practice for relational databases and hierarchies. My question is, is it more sane to use text or int (id) based hierarchies?
I have a hierarchy, but it is not an id based one. The hierarchy is based on text
e.g. ‘level1’, ‘level2’ as opposed to id1, id2
I use mysql and also solr for managing my data.
At the moment I have kept the text hierarchy, so could use that if I want to. But it seems healthier allround to create an id hierarchy (which I have also done). Int (id) hierarchies also seem faster and are not really prone to un-normalized data (I never have to trim() ids etc.)
Any thoughts on this are much appreciated. It would be interesting to find out what others feel is best practice.
Cheers
Ke
I tend to use both, if I need to fetch subtrees.
Rows have 2 int columns consisting of an id and a parentId. This makes up the tree structure.
In addition I also have a textual level, representing the indexes of the parent line. e.g. a row with idName=”44.21.31″ would have an id of 31, a parent id of 21 and its grand parent would have an id of 44. This way you can fetch subtrees, `where idName like “44.21.%” would fetch every child, grand child and so on of the row with id 21.
That does break normal form though, there’s now redundant information about the id of an entity – but it can be worth it, especially for db systems that doesn’t otherwise suport hierarcical structures.