1

I need to represent hierarchy of geographical objects in my database. Each object have a name and geographical coordinates (lat/lon).

The 1st level of hierarchy may be one of two possible values: Marine | Terrastiaral.

2nd level (for Marine) may be Seas | Rivers. 3rd level (for Seas) consists of all possible Sea names (e.g. Baltic Sea).

Additionally, on 3rd level I probably want to link each Sea/River with ocean it relates to, all Rivers should be additionally linked with some Sea it relates to. There are also 4th, 5th and 6th levels of hierarchy which are smaller types of objects.

Important: any combination of [3, 4, 5, 6] levels may be skipped in this type of hierarchy, i.e. we could have levels (1, 2, 3, 4, 5) or (1, 2, 3) or (1, 2, 3, 5).

Also, I will be using sqlalchemy ORM for object representation in my application.

Should I use different tables for different hierarchy levels with foreign keys for parent nodes? For skipped levels we could use virtual nodes for each hierarchy path.

Should I make an unified table for all nodes and store its level as integer value, with FK for parents (i.e. adjacency list structure) ?

What are the pros and cons of each method?

Who have other ideas (taking into account all constraints)?

1
  • Do you want to store different things about each level in the hierarchy, or just the name and lat/long?
    – WW.
    Aug 7, 2013 at 9:51

2 Answers 2

3

It depends on what you are going to do with the data. Your separate tables approach works well for certain kinds of uses.

If you go with a unified table, there are two approaches you could take. The first is an adjacency list model. The adjacency list model in a relational schema doesn't look the same way it does in an object world.

The second is a nested set model. Wikipedia Article. The first is easier to update while the second is easier to use for complex queries, like finding a given subtree.

0

the best table design i can think of is as below

table hirarchy

columns : hirarchyid | hirarchylevel



table hirarchy_possible_values

columns : possiblevalueid | possiblevalue | hirarchyid



table geo_object

columns : objectid | objectName| latitude | longitude  | hirarchyid | possiblevalueid
1
  • How is the parent-child relation is established in your proposed db schema? Also, could you please explain what is the 'hirarchy_possible_values' for?
    – leavittx
    Aug 7, 2013 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.