0

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?

1 Answer 1

0

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
5
  • Thanks for this. So what would the pseudo-SQL be to select all the messages for a given Category? SELECT Message FROM MessageTable WHERE Category = "MyCategory" AND SubCategory = ? May 5, 2014 at 15:15
  • anyone have an answer to my query? May 6, 2014 at 15:47
  • For a given Category the psuedo-SQL would be SELECT Message FROM MessageTable WHERE Category = "MyCategory". If you want to include the sub categories, the psuedo-SQL would be SELECT Message FROM MessageTable WHERE Category = "MyCategory" & SELET Subcategory FROM SubcategoryTable WHERE Category = "MyCategory" May 6, 2014 at 19:35
  • subcategory table does not have a Category field, it does however have the Category Key. If for example, the Category was 'Birthday' and it has a subcategory (30th, 40th, 50th etc). Then how would I bring back all messages where the Category is 'Birthday' and the SubCategory is '60th'?? May 7, 2014 at 9:01
  • This works, although might be tricky to maintain if I insert new messages I will need to also insert into the subcategory table... May 7, 2014 at 15:26

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.