<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dave Rowe's Blog &#187; multi-delete</title>
	<atom:link href="http://blog.roweware.com/tag/multi-delete/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.roweware.com</link>
	<description>Ramblings about things I think I know...</description>
	<lastBuildDate>Wed, 14 Jul 2010 17:45:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MySQL Multi-Table Delete</title>
		<link>http://blog.roweware.com/2009/12/08/mysql-multi-table-delete/</link>
		<comments>http://blog.roweware.com/2009/12/08/mysql-multi-table-delete/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 19:27:20 +0000</pubDate>
		<dc:creator>Dave Rowe</dc:creator>
				<category><![CDATA[Database Design]]></category>
		<category><![CDATA[joins]]></category>
		<category><![CDATA[multi-delete]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://blog.roweware.com/?p=142</guid>
		<description><![CDATA[MySQL provides cascading updates / deletes with the relationships, but I tend not to use them, specifically because I want to control just how far these updates and deletes cascade!  But, given a situation where I have a design similar to this: I would like to be able to remove a single Foo, without having [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL provides cascading updates / deletes with the relationships, but I tend not to use them, specifically because I want to control just how far these updates and deletes cascade!  But, given a situation where I have a design similar to this:</p>
<p style="text-align: center;"><img class="size-full wp-image-143 aligncenter" title="multi-delete" src="http://blog.roweware.com/wp-content/uploads/2009/12/multi-delete.png" alt="multi-delete" width="270" height="255" /></p>
<p>I would like to be able to remove a single Foo, without having to first remove all the associated data from the other 3 tables.  Or, I know the ID of the Foo I want to remove, so instead of running multiple queries to find the associated rows, lets just knock it out with a single, multi-table delete!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p142code2'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1422"><td class="code" id="p142code2"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DELETE</span>
  db<span style="color: #66cc66;">.</span>zap <span style="color: #993333; font-weight: bold;">as</span> z<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>baz <span style="color: #993333; font-weight: bold;">as</span> bz<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>bar <span style="color: #993333; font-weight: bold;">as</span> z<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>foo <span style="color: #993333; font-weight: bold;">as</span> f
<span style="color: #993333; font-weight: bold;">FROM</span>
  db<span style="color: #66cc66;">.</span>zap <span style="color: #993333; font-weight: bold;">as</span> b<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>baz <span style="color: #993333; font-weight: bold;">as</span> bz<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>bar <span style="color: #993333; font-weight: bold;">as</span> z<span style="color: #66cc66;">,</span>
  db<span style="color: #66cc66;">.</span>foo <span style="color: #993333; font-weight: bold;">as</span> f
<span style="color: #993333; font-weight: bold;">WHERE</span>
  b<span style="color: #66cc66;">.</span>baz_id <span style="color: #66cc66;">=</span> bz<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">AND</span>
  b<span style="color: #66cc66;">.</span>zap_id <span style="color: #66cc66;">=</span> z<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">AND</span>
  bz<span style="color: #66cc66;">.</span>foo_id <span style="color: #66cc66;">=</span> f<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">AND</span>
  z<span style="color: #66cc66;">.</span>foo_id <span style="color: #66cc66;">=</span> f<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">AND</span>
  f<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">=</span> ?</pre></td></tr></table></div>

<p>This will then remove the rows associated with the single Foo record I&#8217;ve referenced, in one fell swoop.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roweware.com/2009/12/08/mysql-multi-table-delete/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
