<?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: How to write INSERT IF NOT EXISTS queries in standard  SQL</title>
	<atom:link href="http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/</link>
	<description>Stay curious!</description>
	<lastBuildDate>Thu, 09 Feb 2012 09:56:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: oleg</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19643</link>
		<dc:creator>oleg</dc:creator>
		<pubDate>Mon, 19 Sep 2011 16:39:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=8#comment-19643</guid>
		<description>Jason wrote:

&gt; Here’s one I used that should work in all SQL languages. I was trying to avoid using specific language stuff like EXISTS and DUPLICATE.
&gt;
&gt; It’s very simple. Just insert the value(s) where 0 equals the count of the value(s) already existing.
&gt;
&gt; INSERT INTO audit_type
&gt; SELECT DISTINCT ‘sample’ FROM audit_type WHERE
&gt; 0 = (SELECT COUNT(audit_type_desc) FROM audit_type WHERE audit_type_desc = ‘sample’)

This doesn&#039;t work if audit_type is empty.  Also, it can fail with
uniqueness violation when doing it inside transactions, like I
described one post above.  Would be interesting to know if it can fail
without transactions involved too.</description>
		<content:encoded><![CDATA[<p>Jason wrote:</p>
<p>&gt; Here’s one I used that should work in all SQL languages. I was trying to avoid using specific language stuff like EXISTS and DUPLICATE.<br />
&gt;<br />
&gt; It’s very simple. Just insert the value(s) where 0 equals the count of the value(s) already existing.<br />
&gt;<br />
&gt; INSERT INTO audit_type<br />
&gt; SELECT DISTINCT ‘sample’ FROM audit_type WHERE<br />
&gt; 0 = (SELECT COUNT(audit_type_desc) FROM audit_type WHERE audit_type_desc = ‘sample’)</p>
<p>This doesn&#8217;t work if audit_type is empty.  Also, it can fail with<br />
uniqueness violation when doing it inside transactions, like I<br />
described one post above.  Would be interesting to know if it can fail<br />
without transactions involved too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19637</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Fri, 16 Sep 2011 15:30:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=8#comment-19637</guid>
		<description>Here&#039;s one I used that should work in all SQL languages. I was trying to avoid using specific language stuff like EXISTS and DUPLICATE.

It&#039;s very simple. Just insert the value(s) where 0 equals the count of the value(s) already existing.

INSERT INTO audit_type
SELECT DISTINCT &#039;sample&#039; FROM audit_type WHERE
0 = (SELECT COUNT(audit_type_desc) FROM audit_type WHERE audit_type_desc = &#039;sample&#039;)</description>
		<content:encoded><![CDATA[<p>Here&#8217;s one I used that should work in all SQL languages. I was trying to avoid using specific language stuff like EXISTS and DUPLICATE.</p>
<p>It&#8217;s very simple. Just insert the value(s) where 0 equals the count of the value(s) already existing.</p>
<p>INSERT INTO audit_type<br />
SELECT DISTINCT &#8216;sample&#8217; FROM audit_type WHERE<br />
0 = (SELECT COUNT(audit_type_desc) FROM audit_type WHERE audit_type_desc = &#8216;sample&#8217;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: oleg</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19593</link>
		<dc:creator>oleg</dc:creator>
		<pubDate>Wed, 24 Aug 2011 19:10:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=8#comment-19593</guid>
		<description>This fails in PostgreSQL as soon as another process adds rows to the
urls table inside transactions.  To reproduce:

1. Start process 1 (psql) and process 2 (psql).

2. In both processes, type &quot;\set AUTOCOMMIT on&quot; (this stops psql from
   starting implicit transactions).

3. In process 1, type:

     begin;
     insert into urls(url) values (&#039;http://www.xaprb.com/blog/&#039;);

4. In process 2, type:

     insert into urls(url)
     select &#039;http://www.xaprb.com/blog/&#039;
     from mutex
         left outer join urls
             on urls.url = &#039;http://www.xaprb.com/blog/&#039;
     where mutex.i = 1 and urls.url is null;

5. Process 2 hangs waiting for process 1 to commit or roll back.

6. In process 1, type:

     commit;

7. Behold error in process 2:

     ERROR:  duplicate key value violates unique constraint &quot;urls_pkey&quot;

It&#039;s tricky to test this with two processes without a transaction, but
I suspect this can also fail without any transactions involved.</description>
		<content:encoded><![CDATA[<p>This fails in PostgreSQL as soon as another process adds rows to the<br />
urls table inside transactions.  To reproduce:</p>
<p>1. Start process 1 (psql) and process 2 (psql).</p>
<p>2. In both processes, type &#8220;\set AUTOCOMMIT on&#8221; (this stops psql from<br />
   starting implicit transactions).</p>
<p>3. In process 1, type:</p>
<p>     begin;<br />
     insert into urls(url) values (&#8216;<a href="http://www.xaprb.com/blog/" rel="nofollow">http://www.xaprb.com/blog/</a>&#8216;);</p>
<p>4. In process 2, type:</p>
<p>     insert into urls(url)<br />
     select &#8216;<a href="http://www.xaprb.com/blog/" rel="nofollow">http://www.xaprb.com/blog/</a>&#8216;<br />
     from mutex<br />
         left outer join urls<br />
             on urls.url = &#8216;<a href="http://www.xaprb.com/blog/" rel="nofollow">http://www.xaprb.com/blog/</a>&#8216;<br />
     where mutex.i = 1 and urls.url is null;</p>
<p>5. Process 2 hangs waiting for process 1 to commit or roll back.</p>
<p>6. In process 1, type:</p>
<p>     commit;</p>
<p>7. Behold error in process 2:</p>
<p>     ERROR:  duplicate key value violates unique constraint &#8220;urls_pkey&#8221;</p>
<p>It&#8217;s tricky to test this with two processes without a transaction, but<br />
I suspect this can also fail without any transactions involved.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: oleg</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19493</link>
		<dc:creator>oleg</dc:creator>
		<pubDate>Sat, 16 Jul 2011 00:12:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=8#comment-19493</guid>
		<description>Can somebody please explain why this can&#039;t fail with uniqueness violation on the url column?  If another client commits a record with url = &#039;http://www.xaprb.com/blog/&#039; after our &quot;SELECT ... OUTER JOIN ...&quot; runs but before our INSERT does, then wouldn&#039;t our INSERT later violate uniqueness violation by inserting the duplicate url?  Just like in the Microsoft SQL example with the IF statement?</description>
		<content:encoded><![CDATA[<p>Can somebody please explain why this can&#8217;t fail with uniqueness violation on the url column?  If another client commits a record with url = &#8216;<a href="http://www.xaprb.com/blog/" rel="nofollow">http://www.xaprb.com/blog/</a>&#8216; after our &#8220;SELECT &#8230; OUTER JOIN &#8230;&#8221; runs but before our INSERT does, then wouldn&#8217;t our INSERT later violate uniqueness violation by inserting the duplicate url?  Just like in the Microsoft SQL example with the IF statement?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mPHO</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19321</link>
		<dc:creator>mPHO</dc:creator>
		<pubDate>Tue, 03 May 2011 14:20:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=8#comment-19321</guid>
		<description>Hi guys 

I have the below syntax but it is always insert duplicates can someone please help

INSERT INTO roshcon_vehicle_v1_0 
					(VehicleRegNumber,VehicleType,VehicleMake,driver_FstName,driver_Surname,driver_TelNr1Number,driver_SubName,driver_TelNr4Number,driver_SubManagerEmail ,Hire_Time)
					SELECT roshcon_vehicle_report_001.VehicleRegNumber, VehicleType,VehicleMake,roshcon_vehicle_drivers_v1_0.driver_FstName, roshcon_vehicle_drivers_v1_0.driver_Surname,driver_TelNr1Number,driver_SubName,driver_TelNr4Number,driver_SubManagerEmail , Hire_Time
FROM roshcon_vehicle_driver_v1_0, roshcon_vehicle_report_001, roshcon_vehicle_drivers_v1_0  
WHERE roshcon_vehicle_report_001.Vehicle_Id = roshcon_vehicle_driver_v1_0.Id
AND roshcon_vehicle_drivers_v1_0.driver_Id = roshcon_vehicle_driver_v1_0.driver_FstName</description>
		<content:encoded><![CDATA[<p>Hi guys </p>
<p>I have the below syntax but it is always insert duplicates can someone please help</p>
<p>INSERT INTO roshcon_vehicle_v1_0<br />
					(VehicleRegNumber,VehicleType,VehicleMake,driver_FstName,driver_Surname,driver_TelNr1Number,driver_SubName,driver_TelNr4Number,driver_SubManagerEmail ,Hire_Time)<br />
					SELECT roshcon_vehicle_report_001.VehicleRegNumber, VehicleType,VehicleMake,roshcon_vehicle_drivers_v1_0.driver_FstName, roshcon_vehicle_drivers_v1_0.driver_Surname,driver_TelNr1Number,driver_SubName,driver_TelNr4Number,driver_SubManagerEmail , Hire_Time<br />
FROM roshcon_vehicle_driver_v1_0, roshcon_vehicle_report_001, roshcon_vehicle_drivers_v1_0<br />
WHERE roshcon_vehicle_report_001.Vehicle_Id = roshcon_vehicle_driver_v1_0.Id<br />
AND roshcon_vehicle_drivers_v1_0.driver_Id = roshcon_vehicle_driver_v1_0.driver_FstName</p>
]]></content:encoded>
	</item>
</channel>
</rss>

