<?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>MySQL: INSERT IF NOT EXISTS syntax</title> <atom:link href="http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html/feed" rel="self" type="application/rss+xml" /><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Sat, 19 May 2012 15:27:39 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-138518</link> <dc:creator>Mario</dc:creator> <pubDate>Tue, 03 Jan 2012 10:06:01 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-138518</guid> <description>&quot;Insert Ignore into&quot; increments auto number even in cases that new record is not added.</description> <content:encoded><![CDATA[<p>&#8220;Insert Ignore into&#8221; increments auto number even in cases that new record is not added.</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-138016</link> <dc:creator>KKR</dc:creator> <pubDate>Fri, 16 Dec 2011 20:35:58 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-138016</guid> <description>INSERT into Foo.bar select &quot;va11&quot;, &quot;val2&quot; from dual where not exists (select val1 from Foo.bar where username = &quot;val1&quot;);</description> <content:encoded><![CDATA[<p>INSERT into Foo.bar select &#8220;va11&#8243;, &#8220;val2&#8243; from dual where not exists (select val1 from Foo.bar where username = &#8220;val1&#8243;);</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-137453</link> <dc:creator>Subhendu</dc:creator> <pubDate>Tue, 29 Nov 2011 19:03:25 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-137453</guid> <description>Thanks dude..
After struggling for couple of hours I found this &quot;INSERT IGNORE INTO&quot;.
Thanks A TON</description> <content:encoded><![CDATA[<p>Thanks dude..</p><p>After struggling for couple of hours I found this &#8220;INSERT IGNORE INTO&#8221;.</p><p>Thanks A TON</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-137161</link> <dc:creator>Frank</dc:creator> <pubDate>Mon, 21 Nov 2011 11:23:14 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-137161</guid> <description>Thanks for info:INSERT IGNORE INTO table_name SET col = &#039;value&#039;;
Still, table shall be updated as:
ALTER TABLE `db_name`.`table_name` ADD UNIQUE KEY(col);
(otherwise, the first statement won&#039;t work.)
All the best,</description> <content:encoded><![CDATA[<p>Thanks for info:INSERT IGNORE INTO table_name SET col = &#8216;value&#8217;;<br
/> Still, table shall be updated as:<br
/> ALTER TABLE `db_name`.`table_name` ADD UNIQUE KEY(col);<br
/> (otherwise, the first statement won&#8217;t work.)<br
/> All the best,</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136433</link> <dc:creator>Bill</dc:creator> <pubDate>Fri, 14 Oct 2011 13:19:14 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136433</guid> <description>I have created a page that will automatically add posts to WordPress but every time it runs, it creates duplicate posts. The code string is below. I&#039;ve tried several variations to get the code to update the record if it has changed, otherwise ignore it and add any new records. Any help would be appreciated.
$link = mysql_connect($hostname, $user, $pass);
mysql_select_db($dbname, $link);
$results = mysql_query(&quot;SELECT * FROM genesis&quot;,$link);
require(&#039;./wp-load.php&#039;);</description> <content:encoded><![CDATA[<p>I have created a page that will automatically add posts to WordPress but every time it runs, it creates duplicate posts. The code string is below. I&#8217;ve tried several variations to get the code to update the record if it has changed, otherwise ignore it and add any new records. Any help would be appreciated.</p><p>$link = mysql_connect($hostname, $user, $pass);<br
/> mysql_select_db($dbname, $link);</p><p>$results = mysql_query(&#8220;SELECT * FROM genesis&#8221;,$link);</p><p>require(&#8216;./wp-load.php&#8217;);</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136294</link> <dc:creator>MySQL if exist update else insert&#160;&#124;&#160;prosoxi.com</dc:creator> <pubDate>Thu, 29 Sep 2011 11:40:25 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136294</guid> <description>[...] http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html [...]</description> <content:encoded><![CDATA[<p>[...] <a
href="http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html" rel="nofollow">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html</a> [...]</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136231</link> <dc:creator>Pope</dc:creator> <pubDate>Tue, 20 Sep 2011 17:37:19 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136231</guid> <description>You are my Hero!</description> <content:encoded><![CDATA[<p>You are my Hero!</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136096</link> <dc:creator>Chris</dc:creator> <pubDate>Fri, 09 Sep 2011 17:29:14 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136096</guid> <description>cheers - just saved me a real headache. Was considering doing a SELECT for every record, checking for existence, then making a decision based on that... man, sometimes I go round the houses when a simple solution is right there in front of me.</description> <content:encoded><![CDATA[<p>cheers &#8211; just saved me a real headache. Was considering doing a SELECT for every record, checking for existence, then making a decision based on that&#8230; man, sometimes I go round the houses when a simple solution is right there in front of me.</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-135978</link> <dc:creator>Thanks</dc:creator> <pubDate>Mon, 29 Aug 2011 04:22:10 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-135978</guid> <description>Thanks man.. first result on google, no need to go anywhere else</description> <content:encoded><![CDATA[<p>Thanks man.. first result on google, no need to go anywhere else</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-134556</link> <dc:creator>Claude Quézel</dc:creator> <pubDate>Fri, 18 Feb 2011 03:54:10 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-134556</guid> <description>Note that the mysql doc states:
If the statement updates a row instead, LAST_INSERT_ID() is not meaningful. However, you can work around this by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;</description> <content:encoded><![CDATA[<p>Note that the mysql doc states:</p><p>If the statement updates a row instead, LAST_INSERT_ID() is not meaningful. However, you can work around this by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows:</p><p>INSERT INTO table (a,b,c) VALUES (1,2,3)<br
/> ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-114862</link> <dc:creator>vivin</dc:creator> <pubDate>Thu, 30 Sep 2010 05:17:46 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-114862</guid> <description>nice thank u!!!</description> <content:encoded><![CDATA[<p>nice thank u!!!</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-113818</link> <dc:creator>ViserExcizer</dc:creator> <pubDate>Sun, 19 Sep 2010 04:42:08 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-113818</guid> <description>wundervoll! thanks for this</description> <content:encoded><![CDATA[<p>wundervoll! thanks for this</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-112580</link> <dc:creator>ienaxxx</dc:creator> <pubDate>Wed, 08 Sep 2010 15:19:31 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-112580</guid> <description>Very nice article, really useful</description> <content:encoded><![CDATA[<p>Very nice article, really useful</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-108462</link> <dc:creator>Bogdan</dc:creator> <pubDate>Fri, 16 Jul 2010 12:04:36 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-108462</guid> <description>MySQL-specific solution would be to use &lt;a href=&quot;http://dev.mysql.com/doc/refman/5.1/en/example-auto-increment.html&quot; rel=&quot;nofollow&quot;&gt;autoincrement&lt;/a&gt; (linked page also has an example for a two-component primary key, something like your X and Y; see also comments there for more detailed explanation).
Actually, do think if you really want those X and Y serve the purpose of unique IDs... maybe it is better having a single ID, and storing X and Y as values?
In any case, you could implement X/Y-incrementing logic in your app completely DB-engine independently, e.g.
&lt;code&gt;
X.new = SELECT MAX(X) + 1 FROM table;
Y.new = SELECT MAX(Y) + 1 FROM table;
INSERT INTO table(X, Y, note) VALUES (X.new, Y.new, note);
&lt;/code&gt;
However, in this case you should be aware of possible race conditions, if there are several processes possibly writing to that table. To avoid that, you will need table locking. Some apps (e.g. Gallery2) maintain separate tables (sequences) of incremental IDs, to avoid locking data-tables in favour of locking only sequence-tables...
Better use a single autoincrement ID :)</description> <content:encoded><![CDATA[<p>MySQL-specific solution would be to use <a
href="http://dev.mysql.com/doc/refman/5.1/en/example-auto-increment.html" rel="nofollow">autoincrement</a> (linked page also has an example for a two-component primary key, something like your X and Y; see also comments there for more detailed explanation).</p><p>Actually, do think if you really want those X and Y serve the purpose of unique IDs&#8230; maybe it is better having a single ID, and storing X and Y as values?</p><p>In any case, you could implement X/Y-incrementing logic in your app completely DB-engine independently, e.g.<br
/> <code><br
/> X.new = SELECT MAX(X) + 1 FROM table;<br
/> Y.new = SELECT MAX(Y) + 1 FROM table;<br
/> INSERT INTO table(X, Y, note) VALUES (X.new, Y.new, note);<br
/> </code><br
/> However, in this case you should be aware of possible race conditions, if there are several processes possibly writing to that table. To avoid that, you will need table locking. Some apps (e.g. Gallery2) maintain separate tables (sequences) of incremental IDs, to avoid locking data-tables in favour of locking only sequence-tables&#8230;</p><p>Better use a single autoincrement ID <img
src='http://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-108456</link> <dc:creator>cutesmyls</dc:creator> <pubDate>Fri, 16 Jul 2010 06:12:57 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-108456</guid> <description>Hi!
I have a note table, each note has a respective x and y positions..and everytime i add/create a new note, the newly added note should have a unique x and y values in the db..can you suggest on how to do this on sql?</description> <content:encoded><![CDATA[<p>Hi!</p><p>I have a note table, each note has a respective x and y positions..and everytime i add/create a new note, the newly added note should have a unique x and y values in the db..can you suggest on how to do this on sql?</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-106751</link> <dc:creator>Bogdan</dc:creator> <pubDate>Fri, 11 Jun 2010 23:24:03 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-106751</guid> <description>Using sub-query most likely will be slower than all the other solutions (because sub-queries will use temporary tables).
Also, MySQL INSERT...SELECT manual page says: &lt;a href=&quot;http://dev.mysql.com/doc/refman/5.1/en/insert-select.html&quot; rel=&quot;nofollow&quot;&gt;However, you cannot insert into a table and select from the same table in a subquery&lt;/a&gt;. Thus, your solution may not work at all.</description> <content:encoded><![CDATA[<p>Using sub-query most likely will be slower than all the other solutions (because sub-queries will use temporary tables).</p><p>Also, MySQL INSERT&#8230;SELECT manual page says: <a
href="http://dev.mysql.com/doc/refman/5.1/en/insert-select.html" rel="nofollow">However, you cannot insert into a table and select from the same table in a subquery</a>. Thus, your solution may not work at all.</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-106739</link> <dc:creator>Server</dc:creator> <pubDate>Fri, 11 Jun 2010 18:29:33 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-106739</guid> <description>Also i&#039;m using
INSERT INTO `table` (value1,value2) SELECT &#039;stuff for value1&#039;,&#039;stuff for value2&#039; WHERE NOT EXISTS (SELECT * FROM table WHERE value1=&#039;stuff for value1&#039; AND value2=&#039;stuff for value2&#039;) LIMIT 1</description> <content:encoded><![CDATA[<p>Also i&#8217;m using</p><p>INSERT INTO `table` (value1,value2) SELECT &#8216;stuff for value1&#8242;,&#8217;stuff for value2&#8242; WHERE NOT EXISTS (SELECT * FROM table WHERE value1=&#8217;stuff for value1&#8242; AND value2=&#8217;stuff for value2&#8242;) LIMIT 1</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-105313</link> <dc:creator>Alex</dc:creator> <pubDate>Thu, 22 Apr 2010 06:53:39 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-105313</guid> <description>&lt;code&gt;
REPLACE INTO `transcripts`
SET `ensembl_transcript_id` = &#039;ENSORGT00000000001&#039;,
`transcript_chrom_start` = 12345,
`transcript_chrom_end` = 12678;
&lt;/code&gt;
It&#039;s important to note that unless you mention all fields and have autogenerated fields (such as autoincremented primary key), you get to screw up your DB with this method:
I tried
&lt;code&gt;
REPLACE INTO images SET filename=&#039;variable&#039;
&lt;/code&gt;
and guess what, since a record existed, its autoincremened ID got changed.
So watch out for such mistakes: my error may be your lesson.</description> <content:encoded><![CDATA[<p><code><br
/> REPLACE INTO `transcripts`<br
/> SET `ensembl_transcript_id` = 'ENSORGT00000000001',<br
/> `transcript_chrom_start` = 12345,<br
/> `transcript_chrom_end` = 12678;<br
/> </code></p><p>It&#8217;s important to note that unless you mention all fields and have autogenerated fields (such as autoincremented primary key), you get to screw up your DB with this method:</p><p>I tried<br
/> <code><br
/> REPLACE INTO images SET filename='variable'<br
/> </code></p><p>and guess what, since a record existed, its autoincremened ID got changed.<br
/> So watch out for such mistakes: my error may be your lesson.</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-104542</link> <dc:creator>Bogdan</dc:creator> <pubDate>Tue, 09 Mar 2010 15:46:15 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-104542</guid> <description>Thanks, that was an interesting comparison. I only had tens of thousands of rows, so didn&#039;t pay attention to performance.</description> <content:encoded><![CDATA[<p>Thanks, that was an interesting comparison. I only had tens of thousands of rows, so didn&#8217;t pay attention to performance.</p> ]]></content:encoded> </item> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-104541</link> <dc:creator>Eddie</dc:creator> <pubDate>Tue, 09 Mar 2010 15:07:46 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-104541</guid> <description>I did some speed benchmarking of these methods and found that ON DUPLICATE KEY is the faster that INSERT IGNORE.  I also found that just letting the query fail because of a primary key constraint is faster than both.  For my application, anyway.  Your mileage may vary.  More info in my &quot;website&quot; link.</description> <content:encoded><![CDATA[<p>I did some speed benchmarking of these methods and found that ON DUPLICATE KEY is the faster that INSERT IGNORE.  I also found that just letting the query fail because of a primary key constraint is faster than both.  For my application, anyway.  Your mileage may vary.  More info in my &#8220;website&#8221; link.</p> ]]></content:encoded> </item> </channel> </rss>
