<?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: MySQL: INSERT IF NOT EXISTS syntax</title> <atom:link href="https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Mon, 01 Jan 2024 17:12:20 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>By: &#8216;insert if not exists&#8217; in MySQL &#124; Coding and Programing</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-248962</link> <dc:creator><![CDATA[&#8216;insert if not exists&#8217; in MySQL &#124; Coding and Programing]]></dc:creator> <pubDate>Tue, 03 Jun 2014 03:47:21 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-248962</guid> <description><![CDATA[[&#8230;] see http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html [&#8230;]]]></description> <content:encoded><![CDATA[<p>[&#8230;] see <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> [&#8230;]</p> ]]></content:encoded> </item> <item><title>By: Bogdan</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-192245</link> <dc:creator><![CDATA[Bogdan]]></dc:creator> <pubDate>Sat, 31 Aug 2013 14:07:01 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-192245</guid> <description><![CDATA[I&#039;ve increased font size a little, should be better now. What is your screen resolution? Is it higher than 1920x1080 or 1920x1200?]]></description> <content:encoded><![CDATA[<p>I&#8217;ve increased font size a little, should be better now. What is your screen resolution? Is it higher than 1920&#215;1080 or 1920&#215;1200?</p> ]]></content:encoded> </item> <item><title>By: Sabby</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-192170</link> <dc:creator><![CDATA[Sabby]]></dc:creator> <pubDate>Sat, 31 Aug 2013 07:09:03 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-192170</guid> <description><![CDATA[Very useful and best information, only thing is that site fonts are very small. We cannot read it. INSERT IGNORE INTO `transcripts`
SET `ensembl_transcript_id` = &#039;ENSORGT00000000001&#039;,
`transcript_chrom_start` = 12345,
`transcript_chrom_end` = 12678;
I hope you understand what I mean, ignore my comment just make the changes.
Thank you.]]></description> <content:encoded><![CDATA[<p>Very useful and best information, only thing is that site fonts are very small. We cannot read it. INSERT IGNORE INTO `transcripts`<br
/> SET `ensembl_transcript_id` = &#8216;ENSORGT00000000001&#8242;,<br
/> `transcript_chrom_start` = 12345,<br
/> `transcript_chrom_end` = 12678;<br
/> I hope you understand what I mean, ignore my comment just make the changes.<br
/> Thank you.</p> ]]></content:encoded> </item> <item><title>By: MySQL INSERT IF NOT EXISTS: Checking whether a record exists and inserting &#187; What Is Internet?</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-151825</link> <dc:creator><![CDATA[MySQL INSERT IF NOT EXISTS: Checking whether a record exists and inserting &#187; What Is Internet?]]></dc:creator> <pubDate>Sat, 30 Mar 2013 09:21:41 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-151825</guid> <description><![CDATA[[...] Bogdan has the answersÂ . [...]]]></description> <content:encoded><![CDATA[<p>[...] Bogdan has the answersÂ . [...]</p> ]]></content:encoded> </item> <item><title>By: Mario</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-138518</link> <dc:creator><![CDATA[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><![CDATA[&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>By: KKR</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-138016</link> <dc:creator><![CDATA[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><![CDATA[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>By: Subhendu</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-137453</link> <dc:creator><![CDATA[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><![CDATA[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>By: Frank</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-137161</link> <dc:creator><![CDATA[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><![CDATA[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>By: Bill</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136433</link> <dc:creator><![CDATA[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><![CDATA[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>By: MySQL if exist update else insert&#160;&#124;&#160;prosoxi.com</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136294</link> <dc:creator><![CDATA[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><![CDATA[[...] 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>By: Pope</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136231</link> <dc:creator><![CDATA[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><![CDATA[You are my Hero!]]></description> <content:encoded><![CDATA[<p>You are my Hero!</p> ]]></content:encoded> </item> <item><title>By: Chris</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-136096</link> <dc:creator><![CDATA[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><![CDATA[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>By: Thanks</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-135978</link> <dc:creator><![CDATA[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><![CDATA[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>By: Claude QuÃ©zel</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-134556</link> <dc:creator><![CDATA[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><![CDATA[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>By: vivin</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-114862</link> <dc:creator><![CDATA[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><![CDATA[nice thank u!!!]]></description> <content:encoded><![CDATA[<p>nice thank u!!!</p> ]]></content:encoded> </item> <item><title>By: ViserExcizer</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-113818</link> <dc:creator><![CDATA[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><![CDATA[wundervoll! thanks for this]]></description> <content:encoded><![CDATA[<p>wundervoll! thanks for this</p> ]]></content:encoded> </item> <item><title>By: ienaxxx</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-112580</link> <dc:creator><![CDATA[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><![CDATA[Very nice article, really useful]]></description> <content:encoded><![CDATA[<p>Very nice article, really useful</p> ]]></content:encoded> </item> <item><title>By: Bogdan</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-108462</link> <dc:creator><![CDATA[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><![CDATA[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="https://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p> ]]></content:encoded> </item> <item><title>By: cutesmyls</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-108456</link> <dc:creator><![CDATA[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><![CDATA[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>By: Bogdan</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-106751</link> <dc:creator><![CDATA[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><![CDATA[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> </channel> </rss>