views:

111

answers:

3

I am having categories as following,

  • Fun
    • Jokes
    • Comedy
  • Action
    • Movies
    • TV Shows

Now One video can have multiple categories or sub categories, let's say VideoId: 23 is present in Categories Fun, Fun->Comedy, Action->TV Shows but not in Action category. Now I am not getting idea that hwo should I maintain these categories in Database. Should I create only one column "CategoryId AS VARCHAR" in Videos and add category id as comma-separated values (1,3,4) like this but then how I will fetch the records if someone is browsing category Jokes?

Or should I create another table which will have videoId and categoryid, in that case if a Video is present in 3 different categories then 3 rows will be added to that new table

Please suggest some way of how to maintain categories for a particular record in the table

Thanks

+1  A: 

You categories table could have a column in it called parentID that reference another entry in the categories table. It would be a foreign key to itself. NULL would represent a top-level category. Something other then NULL would represent "I am a child category of this category". You could assign a video to any category still, top-level, child, or somewhere inbetween.

Also, use autoincrement notnull integers for your primary keys, not varchar. It's a performance consideration.


To answer your comment:
3 tables: Videos, Categories, and Video_Category

Video_Category would have VideoID and CategoryID columns. The primary key would be a combination of the two columns (a compound primary key)

colithium
That I am maintaining properly, I want how should I maintain a video in different categories, if a video is in the 3 categories then, how I'll know that in how many categories a particular video is present???
Prashant
Refer to the edit I just made
colithium
Yups, the same solution I was thinking to create a new table "Video_Category", but I was not aware that I am doing it right or not. Thanks for your answer.
Prashant
It's exactly right. It's referred to as a "Join Table" and is how you represent a many-to-many relationship in a database
colithium
+1  A: 

You have two choices, parentID (better as INT) to refer to the parent or an extra table with categoryID - parentID. The last one may provide a better logical separation and allows you to have multiple categories.

tuergeist
but if my video is in 3 or 4 or may be 5 different categories then, according to your answer, i'll have to add 5 columns to my table ??? If so, then it will not be an optimized solution, it will just fill my table with columns.....
Prashant
sorry, I changed "columns" to table" ...The core idea was the same as colithium now added to his original answer.
tuergeist
You can add n categories with m parent categories
tuergeist
The same applies for entries. Make an extra table with entryID and categoryID, then you can add every entry to all categories, if you want.
tuergeist
+1  A: 

I suggest that create another table which will have videoId and categoryid. Then you can use sql-query as follow:

select a.*,GROUP_CONCAT(b.category_id) as cagegory_ids 
from table_video a
left join table_video_category b on a.video_id=b.video_id 
group by a.video_id
ZA