Forum Moderators: open
Movie_Table
movie_id
movie
category_id
Category_Table
category_id
category
In the example above, each movie can only have 1 category. How can I make it so that each movie can have multiple categories?
I heard that I need to add another table to make a many-to-many relationship, thus:
Movie_Table
movie_id
movie
movie_category_id
Category_Table
category_id
category
Movie_Category_Table
movie_category_id
movie_id
category_id
What I don't understand is, with this structure, movie_id can still only be associated with 1 category_id. Am I correct?
According to your instructions, my database should look like this:
Movie_Table
movie_id
movie
Category_Table
category_id
category
Movie_Category_Table
movie_id
category_id
What I don't understand is, with only two columns in Movie_Category_Table, the only way I can get a movie to be associated with more than one category is if I entered the movie multiple times, each time with a different category_id.
Is that how it's meant to be? Or have I misunderstood you?
Movie_Table
movie_id movie
1........Titanic
2........Aliens
Category_Table
category_id category
1...........Love Story
2...........Adventure
3...........Drama
4...........Sci Fi
5...........Horror
Movie_Category_Table
movie_id category_id
1.........1
1.........2
1.........3
2.........4
2.........5
I done what you said and it worked. However, it also worked when I tried both a LEFT JOIN and a RIGHT JOIN, thus:
-------------
SELECT movie, category FROM Category_Table, Movie_Table
LEFT JOIN Movie_Category_Table on Movie_Table.movie_id=Movie_Category_Table.movie_id
WHERE
Movie_Category_Table.category_id=Category_Table.category_id
AND
Movie_Category_Table.movie_id='1';
-------------
SELECT movie, category FROM Category_Table, Movie_Table
RIGHT JOIN Movie_Category_Table on Movie_Table.movie_id=Movie_Category_Table.movie_id
WHERE
Movie_Category_Table.category_id=Category_Table.category_id
AND
Movie_Category_Table.movie_id='1';
-------------
Does it matter which way I do it?
For example, to find all of the categories for all movies you might do something like:
SELECT m.movie_id, m.movie, c.category_id, c.category
FROM Movie_Table m
JOIN Movie_Category_Table mc ON m.movie_id = mc.movie_id
JOIN Category_Table c ON mc.category_id = c.category_id
ORDER BY m.movie, c.category
To find all categories for a particular movie (by movie_id) you might use something like:
SELECT m.movie_id, m.movie, c.category_id, c.category
FROM Movie_Table m
JOIN Movie_Category_Table mc ON m.movie_id = mc.movie_id
JOIN Category_Table c ON mc.category_id = c.category_id
WHERE m.movie_id = 123
ORDER BY m.movie, c.category
To get a list of movies that are in a particular category (by category_id) you might do something like:
SELECT m.movie_id, m.movie, c.category_id, c.category
FROM Movie_Table m
JOIN Movie_Category_Table mc ON m.movie_id = mc.movie_id
JOIN Category_Table c ON mc.category_id = c.category_id
WHERE m.category_id = 456
ORDER BY m.movie, c.category
The key is to get from a movie to their category (or categories) or to get from a category to a movie (or movies) you have to go through your relationship table (Movie_Category_Table). So you SELECT FROM Movie_Table then JOIN to Movie_Category_table using movie_id then JOIN to Category_Table using category_id. Or you can do the reverse where you SELECT FROM Category_table then JOIN to Movie_Category_Table using category_id then JOIN to Movie_Table using movie_id.
You cannot just JUMP from Movie_Table straight to Category_Table or visa versa. You go through the relationship table Movie_Category_Table anytime you need to know the categories a movie is in or which movies are in a category.
Hope that helps...
[edited by: ZydoSEO at 5:48 pm (utc) on Sep. 24, 2008]