<?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>devexp &#187; duplicate key</title>
	<atom:link href="http://www.devexp.eu/tag/duplicate-key/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devexp.eu</link>
	<description>DEVelopment EXPerience, shared with the world!</description>
	<lastBuildDate>Fri, 28 Oct 2011 12:07:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>forgotten syntaxes for MySQL</title>
		<link>http://www.devexp.eu/2009/03/31/forgotten-syntaxes-for-mysql/</link>
		<comments>http://www.devexp.eu/2009/03/31/forgotten-syntaxes-for-mysql/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 11:25:26 +0000</pubDate>
		<dc:creator>van Rumste Kenneth</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[delayed]]></category>
		<category><![CDATA[duplicate key]]></category>
		<category><![CDATA[infile]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[option]]></category>
		<category><![CDATA[outfile]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://www.devexp.eu/?p=527</guid>
		<description><![CDATA[A lot of people seem to have problems with updating or inserting data in tables in an intelligent way. We shouldn&#8217;t point a finger to those who don’t use the correct syntax for the problem it solves, because there are &#8230; <a href="http://www.devexp.eu/2009/03/31/forgotten-syntaxes-for-mysql/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><img class="alignright" style="border: 0pt none; margin: 5px;" title="MySQL" src="http://dev.mysql.com/common/logos/logo_mysql_sun_a.gif" alt="" width="114" height="68" />A lot of people seem to have problems with updating or inserting data in tables in an intelligent way. We shouldn&#8217;t point a finger to those who don’t use the correct syntax for the problem it solves, because there are a lot of different ways to do this, but I wanted to create a little list of interesting syntax&#8217;s that are often forgotten and never used. I will talk about:</p>
<ul>
<li>insert&#8230; on duplicate key update</li>
<li>replace</li>
<li>insert&#8230; select</li>
<li>Load data infile and select into outfile</li>
<li>delayed</li>
</ul>
<p><span id="more-527"></span></p>
<p>When starting with SQL a few years ago I always executed a query to get a row out of a table and then updated or insert a row in a second query. Of course there are better solutions for this problem according to the specific action you want to do:</p>
<p><strong>INSERT&#8230; ON DUPLICATE KEY UPDATE</strong><br />
In this case it is possible for anyone to insert a row in a table, but if the key of this row already exists, it updates a specific value:</p>
<pre class="brush: sql; title: ; notranslate">INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;</pre>
<p><strong>REPLACE</strong><br />
Replace works exactly like insert, except that if a row already exists, for the unique index or primary key, it is first deleted and then inserted as a new row.<br />
If you have a table where there is no primary key or index, the replace statement makes absolutely no sense, as there will be nothing to delete.</p>
<pre class="brush: sql; title: ; notranslate">REPLACE INTO table_1 SET a=1, b=2;</pre>
<p>The difference between on duplicate key and replace is:<br />
‘Insert … on duplicate key update’ does an insert or an update.<br />
‘Replace’ does and insert with a possible delete first.</p>
<p><strong>INSERT … SELECT</strong><br />
This will insert rows from one or more tables into another table quickly, again to avoid the fact that you should write to queries to execute this.<br />
Use ‘ignored’ in the syntax to ignore duplicate-key violations</p>
<p><strong>LOAD DATA INFILE and SELECT … INTO OUTFILE</strong><br />
‘Load data infile’ will read data from a file into table at very high speed and ‘select into outfile’ will do the exact opposite, writing data from a table to a file.</p>
<p><strong>DELAYED</strong><br />
When you have users on your application that don’t have the time to wait this is a very handy option for the insert statement. It is generally known that the insert statement takes a lot of time during the execution of it, and with this option you can create the opportunity for your user to directly go on with other actions. The insert will only be queued until the table isn’t being used by another thread. Another benefit is that huge inserts can be executed at once and this will result in a much faster execution time.</p>
<div class="shr-publisher-527"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.devexp.eu/2009/03/31/forgotten-syntaxes-for-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

