Question

I want to design a simple database which contains messages. Each message has a Category. Each message can have one or more Subcategories, but it may not have a subcategory at all.

CategoryTable
CategoryKey, Category

SubCategoryTable
SubCategoryKey, CategoryKey, SubCategory,

MessageTable
MessageKey, SubCategoryKey, Message,

The problem with this design is that if a Message Category has no subcategory, how can I retrieve the messages for that Category?

Whats the best approach for this? Should I have a "None" Subcategory?

Was it helpful?

Solution

Change the Message Table so it points to the Category table.

MessageTable
------------
MessageKey
CategoryKey
Message

Add a MessageSubcategory Table for the one or more sub categories a message might have.

MessageSubCategoryTable
-----------------------
MessageKey
SubCategoryKey

You can get any SubCategories with the following psudeo-SQL:

Select Subcategory
From SubCategoryTable, MessageSubCategoryTable
Where CategoryKey = CategoryKey from MessageTable 
And SubCategoryKey = SubCategoryKey from MessageSubCategoryTable
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top