<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Why have REPLACE INTO and INSERT &#8230; ON DUPLICATE KEY UPDATE?</title>
	<atom:link href="http://www.rustyrazorblade.com/2007/01/why-have-replace-into-and-insert-on-duplicate-key-update/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rustyrazorblade.com/2007/01/why-have-replace-into-and-insert-on-duplicate-key-update/</link>
	<description>Tech Thoughts, Mostly on LAMP - by Jon Haddad</description>
	<lastBuildDate>Mon, 23 Jan 2012 09:03:48 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.rustyrazorblade.com/2007/01/why-have-replace-into-and-insert-on-duplicate-key-update/comment-page-1/#comment-3594</link>
		<dc:creator>Xaprb</dc:creator>
		<pubDate>Thu, 18 Jan 2007 11:34:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2007/01/17/why-have-replace-into-and-insert-on-duplicate-key-update/#comment-3594</guid>
		<description>They&#039;re not equivalent; ODKU allows you to specify exactly which fields get updated, where REPLACE wipes out all existing data and makes a new row.  Sometimes you want this, for example when you have a timestamp column and don&#039;t want to explicitly set it.  Most queries can be written with either syntax, but in practice sometimes I find one or the other solves the problem better.  I generally do not use REPLACE.</description>
		<content:encoded><![CDATA[<p>They&#8217;re not equivalent; ODKU allows you to specify exactly which fields get updated, where REPLACE wipes out all existing data and makes a new row.  Sometimes you want this, for example when you have a timestamp column and don&#8217;t want to explicitly set it.  Most queries can be written with either syntax, but in practice sometimes I find one or the other solves the problem better.  I generally do not use REPLACE.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pabloj</title>
		<link>http://www.rustyrazorblade.com/2007/01/why-have-replace-into-and-insert-on-duplicate-key-update/comment-page-1/#comment-3591</link>
		<dc:creator>pabloj</dc:creator>
		<pubDate>Thu, 18 Jan 2007 08:07:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2007/01/17/why-have-replace-into-and-insert-on-duplicate-key-update/#comment-3591</guid>
		<description>Think you don&#039;t have a primary key and want to replace a tuple which might exist (or not) in that table ...</description>
		<content:encoded><![CDATA[<p>Think you don&#8217;t have a primary key and want to replace a tuple which might exist (or not) in that table &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guillermo Roditi</title>
		<link>http://www.rustyrazorblade.com/2007/01/why-have-replace-into-and-insert-on-duplicate-key-update/comment-page-1/#comment-3589</link>
		<dc:creator>Guillermo Roditi</dc:creator>
		<pubDate>Thu, 18 Jan 2007 03:24:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2007/01/17/why-have-replace-into-and-insert-on-duplicate-key-update/#comment-3589</guid>
		<description>REPLACE is dangerous with InnoDB and cascading DELETEs if something references that row. It will also change the auto_increment of the record. 

ON DUPLICATE also allows for more freedom because, while you may opt to update with the values of the INSERT clause you are not tied down to that. You can use any kind of complex expression in order to achieve different results upon INSERT or UPDATE

FYI update is not always faster! sometimes a DELETE followed by an INSERT may actually be faster.

I use DUPLICATE  very often, yet I have yet to find use for REPLACE in an environment that enforces referential integrity</description>
		<content:encoded><![CDATA[<p>REPLACE is dangerous with InnoDB and cascading DELETEs if something references that row. It will also change the auto_increment of the record. </p>
<p>ON DUPLICATE also allows for more freedom because, while you may opt to update with the values of the INSERT clause you are not tied down to that. You can use any kind of complex expression in order to achieve different results upon INSERT or UPDATE</p>
<p>FYI update is not always faster! sometimes a DELETE followed by an INSERT may actually be faster.</p>
<p>I use DUPLICATE  very often, yet I have yet to find use for REPLACE in an environment that enforces referential integrity</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Stubbs</title>
		<link>http://www.rustyrazorblade.com/2007/01/why-have-replace-into-and-insert-on-duplicate-key-update/comment-page-1/#comment-3588</link>
		<dc:creator>Jason Stubbs</dc:creator>
		<pubDate>Thu, 18 Jan 2007 02:54:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.rustyrazorblade.com/index.php/2007/01/17/why-have-replace-into-and-insert-on-duplicate-key-update/#comment-3588</guid>
		<description>Purely hypothetical, but...

CREATE TABLE foo (
   mykey INT PRIMARY KEY,
   cola CHAR(1) DEFAULT &#039;A&#039;,
   colb CHAR(2) DEFAULT &#039;B&#039;
);
INSERT INTO foo (mykey, cola, colb)
    VALUES (1, &#039;X&#039;, &#039;Y&#039;), (2, &#039;X&#039;, &#039;Y&#039;);
INSERT INTO foo (mykey, cola) VALUES (1, &#039;Z&#039;) ON DUPLICATE KEY UPDATE cola=&#039;Z&#039;;
REPLACE INTO foo (mykey, cola) VALUES (2, &#039;Z&#039;);

We should now have (1, &#039;Z&#039;, &#039;Y&#039;), (2, &#039;Z&#039;, &#039;B&#039;). Of course, this could still be done with UPDATE by appending colb=DEFAULT. The maintainability of that would decrease though. Not that relying on REPLACE to set fields back to their default values is very maintainable either. ;)

Real world case? Nope, can&#039;t think of one either.</description>
		<content:encoded><![CDATA[<p>Purely hypothetical, but&#8230;</p>
<p>CREATE TABLE foo (<br />
   mykey INT PRIMARY KEY,<br />
   cola CHAR(1) DEFAULT &#8216;A&#8217;,<br />
   colb CHAR(2) DEFAULT &#8216;B&#8217;<br />
);<br />
INSERT INTO foo (mykey, cola, colb)<br />
    VALUES (1, &#8216;X&#8217;, &#8216;Y&#8217;), (2, &#8216;X&#8217;, &#8216;Y&#8217;);<br />
INSERT INTO foo (mykey, cola) VALUES (1, &#8216;Z&#8217;) ON DUPLICATE KEY UPDATE cola=&#8217;Z';<br />
REPLACE INTO foo (mykey, cola) VALUES (2, &#8216;Z&#8217;);</p>
<p>We should now have (1, &#8216;Z&#8217;, &#8216;Y&#8217;), (2, &#8216;Z&#8217;, &#8216;B&#8217;). Of course, this could still be done with UPDATE by appending colb=DEFAULT. The maintainability of that would decrease though. Not that relying on REPLACE to set fields back to their default values is very maintainable either. <img src='http://www.rustyrazorblade.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Real world case? Nope, can&#8217;t think of one either.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

