views:

92

answers:

3

I am creating an online blog website and for each blog post, I'd like to have the user be able to create/edit/delete their own category so that they may categorize their post.

What is generally considered a good database design for user generated categories?

This is my proposed table design. (Is there a name for this type of db?)

USER_TABLE
user_id (pk), user_name

CATEGORY_TABLE
category_id (pk), category_name

USER_CATEGORIES
user_id (fk), category_id (fk)

Thanks for helping out. I'm confident there's a post somewhere regarding this but I was unable to find it. If this is a dupe please let me know and I will remove this question.

+4  A: 

This is a many to many relationship. This would allow each user to potentially have many different categories and each category to potentially have many different users. This seems like a useful model for what you are trying to do.

jray
+2  A: 

I think your schema looks good. You are keeping the category labels in one table to avoid duplication and then just assigning their IDs to the users.

Matt Wrock
+1  A: 

If what you are trying to do is to have "private" categories for each user then this is fine. If on the other hand categories are supposed to be public (sth like tags on stackoverflow) then you may consider another option - not storing user<->category relationship, instead add field use_counter to category table and use triggers to increment it when category is being used(blog entry is categorized) or decrement when it's "freed" (blog entry is deleted/ its category is removed). When the use_counter reaches 0 - remove the category.

kubal5003
Hey kubal thanks for that info. I'd never thought of implementing it that way before.
justinl