I'm an experienced ATL/COM developer, but I have no dB background. I'm playing around with a toy app using
VB and MS Access in order to learn some of the concepts and procedures. I'd appreciate your opinions on a design problem. The simplified scenario is:
1) I have two conceptual objects, a location and an asset.
2) Locations can have subordinate locations, which themselve can have subordinate locations, etc. I do not wish to restrict the level of nesting.
3) An asset is related to a location. I want the asset to be related to the any location (one with or without a subordinate location)
4) The tree implied by item 2 does not have to be of uniform depth
5) I want to be able to identify all the locations subordinate (at any depth) to a given location
My initial design:
Table Location:
LocationID AutoNumber (primary key)
Location Text
ParentLocation Number (foreign key) <-- proper term?
Points to LocationID
Table Asset:
AssetID AutoNumber (primary key)
Asset Text
LocationID Number
Example location table: (note that it is intentionally filled out unevenly)
USA (parent = NULL)
Michigan (parent = USA)
Macomb County (parent = Michigan)
Washtenaw County (parent = Michigan)
Ann Arbor (parent = Washtenaw County)
4th district (parent = Ann Arbor )
Ypsilanti (parent = Washtenaw County)
Maryland (parent = USA)
Virginia (parent = USA)
Newport News (parent = Virginia)
York County (parent = Virginia)
Yorktown (parent = York County)
Tabb (parent = York County)
Grafton (parent = York County)
Denmark (parent = NULL)
Italy (parent = NULL)
Example asset table: (note that not all assets related to leaf nodes)
(sorry - no creative names here)
asset1, location = Michigan
asset2, location = Newport News
asset3, location = York County
asset4, location = 4th district
asset5, location = Denmark
Example of item 5)
Given the ID for Michigan, I want to get
Macomb County (parent = Michigan)
Washtenaw County (parent = Michigan)
Ann Arbor (parent = Washtenaw County)
4th district (parent = Ann Arbor )
Ypsilanti (parent = Washtenaw County)
None of the several coworkers (with at least some dB experience) that I queried liked the recursive nature of the first table. Their objections, other than the possibility of a loop, were not clear to me. I believe one of them was that many locations would not have a parent, so some field entries are wasted. The alternative was to list relationships in a separate table. Is there a prefered method for this common parent/child relationship? Different options that are favored under certain conditions?
The other problem is item 5. With my limited knowledge, I can only generate this list (from
VB) using a separate query for each level of the "location" tree. Also, the list ends up in a
VB Collection, not a single recordset. My coworkers said this was horrible, and that I should come up with a design that only requires one query (performance issue, perhaps considering distributed dBs?). One alternative I read online was to read in the whole table and do the recursion in
VB code. Although my database will never be very large, I want to learn the concepts, and that doesn't seem to scale very well.
Hope this isn't too much information!! Any advice?
Thanks,
Greg