I would like to create the following hierarchy in a sql db. This is what I have. Is it correct or is there an easier way. Also how do I query it to show the same hierarchy?
Services
Property
Residential
To let
Purchase
Accommodation
Maintenance
Plumbers
Electricians
Carpenters
Industrial
To let
Purchase
Accommodation
Maintenance
Plumbers
Electricians
Carpenters
Table CategoryType
CategoryTypeID CategoryName
1 Services
2 Products
3 Main
4 MainSub
5 Sub
6 Sub1
Table Category
CateogryID CategoryName CategoryTypeID ParentID
1 Property Service
2 Residential Main 1
3 To let MainSub 2
4 Purchase Sub 3
5 Accommodation Sub 4
Your schema may work, but RDBMSs are notoriously difficult to work with when attempting to represent hierarchical data. You’ll either have a very hard time updating the table, or it will be incredibly inefficient to query against it. My suggestion if you’re going to be using such data is to consider a nested set model.
http://en.wikipedia.org/wiki/Nested_set_model
http://www.evanpetersen.com/item/nested-sets.html
There’s a bit of a learning curve, but it’s worth it.
Cheers