Lets say that I have to store the following information in my database,
Now my database tables will be designed and structured like this,
In a later date, if I had to add another sub category level how will I be able to achieve this without having to change the database structure at all?
I have heard of defining the columns as row data in a table and using pivots to extract the details later on...Is that the proper way to achieve this?
Can someone please enlighten me or guide me in the proper direction? Thanks in advance...
:)
It would be difficult to add more columns to your table when new levels are to be generated. The best way is to use a
Hierarchy table
to maintainParent-Child relationship
.Table :
Items
Table :
Category
In category table, you can add categories to
n
levels. InItems
table, you can store the lowest level category. For example, take the case ofPepsi
- itscategoryId
is 3. In Category table, you can find its parent usingJOIN
s and find parent's parents using Hierarchy queries.In
Category
table, the categories withParentId
is null(that is with no parentId) will be theMainCategory
and the other items withParentId
will be underSubCategory
.EDIT :
Any how you need to alter the tables, because as per your current schema, you cannot add column to the first table because the number of Sub category may keep on changing. Even if you create a table as per Rhys Jones answer, you have to join two tables with string. The problem in joining with string is that, when there is a requirement to change the
Sub category
orMain category
name, you have to change in every table which you be fall to trouble in future and is not a good database design. So I suggest you to follow the below pattern.Here is the query that get the parents for child items.
Here is the result from the above query
Explanation
Black Forest
comes underCake
.Cake
comes underBakery
.Bakery
comes underFood
.Like this you can create children or parent for any number of levels. Now if you want to add a parent to
Food
andBeverage
, for eg,Food Industry
, just addFood Industry
toCategory
table and keepFood Industry's
Id asParentId
forFood
andBeverage
. Thats all.Now if you want do pivoting, you can follow the below procedures.
1. Get values from column to show those values as column in pivot
2. Now use the below PIVOT query
You will get the below result after the pivot
NOTE
If you want all the records irrespective of an item, remove the
WHERE
clause insideCTE
. Click here to view result.Now I have provided order of columns in pivot table as
DESC
ie, its shows top-level parent.....Item's parent. If you want to show Item's parent first followed be next level and top-level parent at last, you can changeDESC
inside theROW_NUMBER()
toASC
. Click here to view result.In order to add a new sub category, you should add the category to the table "ItemSubCategory1" after that you can easily add it to the "Drinks" table.
For Example: If there is a new category name "Hot Drinks" and a new item "Coffee" which comes in Beverages main category (let CatId=1, MainCatText='Beverages' in ItemMainCategory table) then
According to your schema there's no relationship between 'main category' and 'sub category' but your sample data suggests there would be a relationship, i.e. Alcohol IS A Beverage etc. This sounds like a hierarchy of categories, in which case you could you a single self-referencing Category table instead;
This way you can create as many levels of category as you want. Any category where ParentCategoryID = CategoryID is a top level category.
Hope this helps,
Rhys