How do I handle (what I think is) a many-to-many relationship in MySql?
May 31, 2004 1:40 PM
Subscribe
MySQL (or generic relational database) question.
I'm building a site for a dvd store and am new to php/mysql. I read Larry Ullman's excellent book PHP and MySQL for Dynamic Web Sites as well as did a search but can't find an answer to this question. It may be because I don't really know how to phrase the question succinctly. Here goes:
I have a table (say, TALENT) which will have various fields including a talent_id field as the key.
I have another table (DVDs) which will have a reference to the talent_id key from the TALENT table.
What I can't seem to get my head around is in the DVDs table, how do I handle instances of multiple actors? Most of the other fields (title, year, etc.) will have one answer, but the talent field in the DVDs table will have many different "answers".
Is this an example of a many-to-many relationship? From the descriptions I've read of M2M, I don't think so, but I can't seem to figure out how to handle the situation... any help, clarity, or links would be appreciated.
posted by dobbs to computers & internet (11 comments total)
then, to get the names of all the talent in a given film, you might do:
select name from talent where talent_id in (select talent_id from cast where film_id = 42)
(or something similar - not used sql for a while). there may be other ways to do it, perhaps better normalised (the cast table doesn't have a single unique key), but i hope this shows you the general idea.
posted by andrew cooke at 1:53 PM on May 31, 2004