<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: How do I handle (what I think is) a many-to-many relationship in MySql?</title>
	<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql/</link>
	<description>Comments on Ask MetaFilter post How do I handle (what I think is) a many-to-many relationship in MySql?</description>
	<pubDate>Mon, 31 May 2004 13:53:22 -0800</pubDate>
	<lastBuildDate>Mon, 31 May 2004 13:53:22 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: How do I handle (what I think is) a many-to-many relationship in MySql?</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql</link>	
		<description>MySQL (or generic relational database) question. &lt;br /&gt;&lt;br /&gt; I&apos;m building a site for a dvd store and am new to php/mysql. I read Larry Ullman&apos;s excellent book PHP and MySQL for Dynamic Web Sites as well as did a search but can&apos;t find an answer to this question. It may be because I don&apos;t really know how to phrase the question succinctly. Here goes:&lt;br&gt;
&lt;br&gt;
I have a table (say, TALENT) which will have various fields including a talent_id field as the key.&lt;br&gt;
&lt;br&gt;
I have another table (DVDs) which will have a reference to the talent_id key from the TALENT table.&lt;br&gt;
&lt;br&gt;
What I can&apos;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 &quot;answers&quot;. &lt;br&gt;
&lt;br&gt;
Is this an example of a many-to-many relationship? From the descriptions I&apos;ve read of M2M, I don&apos;t think so, but I can&apos;t seem to figure out how to handle the situation... any help, clarity, or links would be appreciated.</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2004:site.7617</guid>
		<pubDate>Mon, 31 May 2004 13:40:50 -0800</pubDate>
		<dc:creator>dobbs</dc:creator>
		
			<category>mysql</category>
		
			<category>relationaldatabases</category>
		
	</item> <item>
		<title>By: andrew cooke</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150358</link>	
		<description>you could have an extra table that is called cast, and which has two columns - talent_id and film_id (film_id could be the same as dvd_id, but you might want to worry about having more than one dvd of the same film).&lt;br&gt;
&lt;br&gt;
then, to get the names of all the talent in a given film, you might do:&lt;br&gt;
&lt;br&gt;
select name from talent where talent_id in (select talent_id from cast where film_id = 42)&lt;br&gt;
&lt;br&gt;
(or something similar - not used sql for a while).  there may be other ways to do it, perhaps better normalised (the cast table doesn&apos;t have a single unique key), but i hope this shows you the general idea.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150358</guid>
		<pubDate>Mon, 31 May 2004 13:53:22 -0800</pubDate>
		<dc:creator>andrew cooke</dc:creator>
	</item><item>
		<title>By: whatnotever</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150359</link>	
		<description>It is a many-to-many relation.  Each DVD links to many actors, and each actor links to many DVDs.  One way to handle it is to use a separate linking table with two columns, one for DVD ID, the other for actor ID.  Each row contains a link between a single DVD and a single actor.&lt;br&gt;
&lt;br&gt;
I guess if you&apos;ve already read about many-to-many relations, I don&apos;t need to go into detail you already have.  So maybe we can help you understand better if you say why you don&apos;t think it&apos;s that type?&lt;br&gt;
&lt;br&gt;
As for links, here&apos;s a random link about &lt;a href=&quot;http://www.faqts.com/knowledge_base/view.phtml/aid/5914/fid/52&quot;&gt;many-to-may relations&lt;/a&gt; (but probably nothing you didn&apos;t already know).</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150359</guid>
		<pubDate>Mon, 31 May 2004 13:55:25 -0800</pubDate>
		<dc:creator>whatnotever</dc:creator>
	</item><item>
		<title>By: nakedcodemonkey</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150363</link>	
		<description>Yes, it&apos;s a many-to-many relationship. (On preview, what everyone else said)  Each DVD is related to one or more actors (1:M); each actor is related to one or more DVDs (1:M); those reciprocal 1:M relationships together make the M:M.  Don&apos;t store the talent_id in the DVD table.  &lt;a href=&quot;http://www.frick-cpa.com/ss7/Theory_Relationships.asp#NN&quot;&gt;This link has an basic example and diagram.&lt;/a&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150363</guid>
		<pubDate>Mon, 31 May 2004 14:09:25 -0800</pubDate>
		<dc:creator>nakedcodemonkey</dc:creator>
	</item><item>
		<title>By: nakedcodemonkey</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150370</link>	
		<description>Here&apos;s a good book, by the way, for help with this stuff: &lt;a href=&quot;http://www.amazon.com/exec/obidos/tg/detail/-/0619064625/qid=1086037887/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/103-9987331-8711044?v=glance&amp;s=books&amp;n=507846&quot;&gt;Concepts of Database Management&lt;/a&gt; was very helpful back when I was struggling to wrap my head around this stuff.  As the title implies, it addresses the broader conceptual issues (how to visualize, design, and efficiently query a relational database) rather than a specific RDBMS&apos;s feature set.  It helps you figure out how to break down data usefully, the illustrations are clear, and there are SQL examples in the relevant places.  It&apos;s one of the few old textbooks I&apos;ve saved and continue to refer to.  Paul DuBois&apos; MySQL also has an excellent general intro-to-relational-databases section.  Once you master the base concepts, the rest gets MUCH easier to understand. :-)</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150370</guid>
		<pubDate>Mon, 31 May 2004 14:30:02 -0800</pubDate>
		<dc:creator>nakedcodemonkey</dc:creator>
	</item><item>
		<title>By: dobbs</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150372</link>	
		<description>Okay, thanks, all. I think I understand it now. I think my big problem was not realizing that I needed a third table. &lt;br&gt;
&lt;br&gt;
So, if I&apos;m understanding correctly:&lt;br&gt;
&lt;br&gt;
TALENT TABLE&lt;br&gt;
talent_id&lt;br&gt;
... details&lt;br&gt;
&lt;br&gt;
DVDs TABLE&lt;br&gt;
dvd_id&lt;br&gt;
... details&lt;br&gt;
&lt;br&gt;
CAST_AND_CREW TABLE&lt;br&gt;
talent_id&lt;br&gt;
dvd_id&lt;br&gt;
nothing else&lt;br&gt;
&lt;br&gt;
Then, whenever I want to do a search on the site of any talent, I&apos;m searching the Talent table, finding the name, grabbing the talent id, matching it to the cast and crew table, grabbing the dvd_id and then outputting results from the DVD table?&lt;br&gt;
&lt;br&gt;
Wow, that seems convoluted.  :)&lt;br&gt;
&lt;br&gt;
On preview, thanks makedcodemonkey... you just reminded me that somewhere in the deep, dark, back-hall-of-my-apartment I have a book called Database Design for Mere Mortals from a previous outing into rdbm which failed. Will have to go dig that one up.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150372</guid>
		<pubDate>Mon, 31 May 2004 14:35:13 -0800</pubDate>
		<dc:creator>dobbs</dc:creator>
	</item><item>
		<title>By: yerfatma</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150375</link>	
		<description>&lt;em&gt;Database Design for Mere Mortals&lt;/em&gt; is a terrific primer on this stuff and will explain this situation very well. Everyone else already gave you the answer on how to handle a many-to-many realtionship. If you&apos;re going to store more than just actors in the mapping table (as a convention I usually call those tables something like TALENT_MAP_DVD to make it painfully obvious 6 months from now what the table does), you might want to add a third column that explains what the &quot;talent&quot; did for that DVD (at the risk of overwhelming you, this would probably be just a simple INT or ENUM field that tied into another table like POSITIONS which held items like &apos;actor&apos;, &apos;director&apos;, etc.).</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150375</guid>
		<pubDate>Mon, 31 May 2004 14:47:28 -0800</pubDate>
		<dc:creator>yerfatma</dc:creator>
	</item><item>
		<title>By: andrew cooke</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150377</link>	
		<description>your example is fine.  i don&apos;t know what mysql lets you do, but you might want to define the pair of talent_id and dvd_id together as the key for cast_and_crew.  that&apos;ll give the db engine something to organise things with and save you from accidentally defining the same member of a dvd twice.  i&apos;m sure this is the kind of thing other people&apos;s links explain.&lt;br&gt;
&lt;br&gt;
a chap called celko also has a good reputation for writing db books, but they&apos;re perhaps not very how-to.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150377</guid>
		<pubDate>Mon, 31 May 2004 14:58:58 -0800</pubDate>
		<dc:creator>andrew cooke</dc:creator>
	</item><item>
		<title>By: vacapinta</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150403</link>	
		<description>&lt;i&gt;CAST_AND_CREW TABLE&lt;br&gt;
talent_id&lt;br&gt;
dvd_id&lt;br&gt;
nothing else&lt;/i&gt;&lt;br&gt;
&lt;br&gt;
Keep in mind, dobbs that this structure allows you to also add any data associated with that particular talent in that particular film too, such as: what role they played, compensation, etc.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150403</guid>
		<pubDate>Mon, 31 May 2004 16:07:50 -0800</pubDate>
		<dc:creator>vacapinta</dc:creator>
	</item><item>
		<title>By: SPrintF</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150458</link>	
		<description>Another suggestion: be prepared to throw out your design.&lt;br&gt;
&lt;br&gt;
The nuts and bolts of relational database design are not difficult, but understanding the data and how you want to represent it can be tricky indeed.&lt;br&gt;
&lt;br&gt;
As an example from your &quot;DVD Store&quot; database:&lt;br&gt;
&lt;br&gt;
What constitutes a &quot;DVD&quot; in your DVD table? Seems simple: an individual disc can be identified by a SKU number, right?&lt;br&gt;
&lt;br&gt;
Well, what about different editions (Fullscreen, Widescreen, Director&apos;s Cut, etc)? Different SKU numbers, but they all have the same &quot;cast and crew,&quot; so linking the cast directly to a specific disc will require duplicating the cast info for each version of the disc.&lt;br&gt;
&lt;br&gt;
Maybe you should have a FEATURE table that describes the film (&quot;THE THING&quot;) linked to the &quot;cast and crew&quot; (&quot;directed by Howard Hawks, starring Kenneth Tobey&quot;), and the different DVDs point to the FEATURE.&lt;br&gt;
&lt;br&gt;
(BTW, I&apos;m sitting here looking at a disc that contains _two_ films: VOYAGE TO THE BOTTOM OF THE SEA and FANTASTIC VOYAGE. Now what do you do?)&lt;br&gt;
&lt;br&gt;
The point is: don&apos;t be surprised if you discover that your initial  database design has problems. Design is a &quot;wicked&quot; problem; you need at least a partial solution to really understand the requirements. So, be prepared to start over and re-do it.&lt;br&gt;
&lt;br&gt;
Finally, it wouldn&apos;t hurt to browse through an existing database similar to what you want to design to get ideas. Spend a little time with the &lt;a href=&quot;http://www.imdb.com&quot;&gt;IMDB Database&lt;/a&gt; and note some of the odd things that turn up in the listings. Good luck.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150458</guid>
		<pubDate>Mon, 31 May 2004 21:07:04 -0800</pubDate>
		<dc:creator>SPrintF</dc:creator>
	</item><item>
		<title>By: billsaysthis</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150459</link>	
		<description>&lt;a href=&quot;http://www.fabforce.net/dbdesigner4/index.php&quot;&gt;dbDesigner 4&lt;/a&gt; is a free, open source tool that might help you with design and implementation.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150459</guid>
		<pubDate>Mon, 31 May 2004 21:08:36 -0800</pubDate>
		<dc:creator>billsaysthis</dc:creator>
	</item><item>
		<title>By: dobbs</title>
		<link>http://ask.metafilter.com/7617/How-do-I-handle-what-I-think-is-a-manytomany-relationship-in-MySql#150851</link>	
		<description>Thanks, all.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2004:site.7617-150851</guid>
		<pubDate>Tue, 01 Jun 2004 20:40:14 -0800</pubDate>
		<dc:creator>dobbs</dc:creator>
	</item>
	</channel>
</rss>
