<?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>Autarchy of the Private Cave &#187; exists</title> <atom:link href="https://bogdan.org.ua/tags/exists/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 28 Dec 2022 16:09:04 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>MySQL: INSERT IF NOT EXISTS syntax</title><link>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html</link> <comments>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comments</comments> <pubDate>Thu, 18 Oct 2007 13:20:21 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[database]]></category> <category><![CDATA[example]]></category> <category><![CDATA[examples]]></category> <category><![CDATA[exists]]></category> <category><![CDATA[insert]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[problem]]></category> <category><![CDATA[syntax]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html</guid> <description><![CDATA[To start: as of the latest MySQL, syntax presented in the title is not possible. But there are several very easy ways to accomplish what is expected using existing functionality. There are 3 possible solutions: using INSERT IGNORE, REPLACE, or INSERT &#8230; ON DUPLICATE KEY UPDATE. Imagine we have a table: CREATE TABLE `transcripts` &#40; [&#8230;]]]></description> <content:encoded><![CDATA[<p>To start: as of the latest MySQL, syntax presented in the title is not possible. But there are several very easy ways to accomplish what is expected using existing functionality.</p><p>There are 3 possible solutions: using INSERT IGNORE, REPLACE, or INSERT &#8230; ON DUPLICATE KEY UPDATE.</p><p>Imagine we have a table:</p><div
id="ig-sh-1" class="syntax_hilite"><div
class="code"><ol
class="sql" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #993333; font-weight: bold;">CREATE</span> <span
style="color: #993333; font-weight: bold;">TABLE</span> <span
style="color: #ff0000;">`transcripts`</span> <span
style="color: #66cc66;">&#40;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;<span
style="color: #ff0000;">`ensembl_transcript_id`</span> <span
style="color: #993333; font-weight: bold;">varchar</span><span
style="color: #66cc66;">&#40;</span><span
style="color: #cc66cc;">20</span><span
style="color: #66cc66;">&#41;</span> <span
style="color: #993333; font-weight: bold;">NOT</span> <span
style="color: #993333; font-weight: bold;">NULL</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;<span
style="color: #ff0000;">`transcript_chrom_start`</span> <span
style="color: #993333; font-weight: bold;">int</span><span
style="color: #66cc66;">&#40;</span><span
style="color: #cc66cc;">10</span><span
style="color: #66cc66;">&#41;</span> <span
style="color: #993333; font-weight: bold;">unsigned</span> <span
style="color: #993333; font-weight: bold;">NOT</span> <span
style="color: #993333; font-weight: bold;">NULL</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;<span
style="color: #ff0000;">`transcript_chrom_end`</span> <span
style="color: #993333; font-weight: bold;">int</span><span
style="color: #66cc66;">&#40;</span><span
style="color: #cc66cc;">10</span><span
style="color: #66cc66;">&#41;</span> <span
style="color: #993333; font-weight: bold;">unsigned</span> <span
style="color: #993333; font-weight: bold;">NOT</span> <span
style="color: #993333; font-weight: bold;">NULL</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;<span
style="color: #993333; font-weight: bold;">PRIMARY</span> <span
style="color: #993333; font-weight: bold;">KEY</span> &nbsp;<span
style="color: #66cc66;">&#40;</span><span
style="color: #ff0000;">`ensembl_transcript_id`</span><span
style="color: #66cc66;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #66cc66;">&#41;</span> ENGINE<span
style="color: #66cc66;">=</span>InnoDB <span
style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span
style="color: #66cc66;">=</span>latin1;</div></li></ol></div></div><p>Now imagine that we have an automatic pipeline importing transcripts meta-data from Ensembl, and that due to various reasons the pipeline might be broken at any step of execution. Thus, we need to ensure two things: 1) repeated executions of the pipeline will not destroy our database, and 2) repeated executions will not die due to &#8216;duplicate primary key&#8217; errors.</p><p>Method 1: using REPLACE<br
/> <span
id="more-238"></span><br
/> It&#8217;s very simple:</p><div
id="ig-sh-2" class="syntax_hilite"><div
class="code"><ol
class="sql" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #993333; font-weight: bold;">REPLACE</span> <span
style="color: #993333; font-weight: bold;">INTO</span> <span
style="color: #ff0000;">`transcripts`</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #993333; font-weight: bold;">SET</span> <span
style="color: #ff0000;">`ensembl_transcript_id`</span> <span
style="color: #66cc66;">=</span> <span
style="color: #ff0000;">'ENSORGT00000000001'</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff0000;">`transcript_chrom_start`</span> <span
style="color: #66cc66;">=</span> <span
style="color: #cc66cc;">12345</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff0000;">`transcript_chrom_end`</span> <span
style="color: #66cc66;">=</span> <span
style="color: #cc66cc;">12678</span>;</div></li></ol></div></div><p>If the record exists, it will be overwritten; if it does not yet exist, it will be created.<br
/> However, using this method isn&#8217;t efficient for our case: we do not need to overwrite existing records, it&#8217;s fine just to skip them.</p><p>Method 2: using INSERT IGNORE<br
/> Also very simple:</p><div
id="ig-sh-3" class="syntax_hilite"><div
class="code"><ol
class="sql" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #993333; font-weight: bold;">INSERT</span> <span
style="color: #993333; font-weight: bold;">IGNORE</span> <span
style="color: #993333; font-weight: bold;">INTO</span> <span
style="color: #ff0000;">`transcripts`</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #993333; font-weight: bold;">SET</span> <span
style="color: #ff0000;">`ensembl_transcript_id`</span> <span
style="color: #66cc66;">=</span> <span
style="color: #ff0000;">'ENSORGT00000000001'</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff0000;">`transcript_chrom_start`</span> <span
style="color: #66cc66;">=</span> <span
style="color: #cc66cc;">12345</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff0000;">`transcript_chrom_end`</span> <span
style="color: #66cc66;">=</span> <span
style="color: #cc66cc;">12678</span>;</div></li></ol></div></div><p>Here, if the &#8216;ensembl_transcript_id&#8217; is already present in the database, it will be silently skipped (ignored). (To be more precise, here&#8217;s a quote from MySQL reference manual: &#8220;If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted.&#8221;.) If the record doesn&#8217;t yet exist, it will be created.</p><p>This second method has several potential weaknesses, including non-abortion of the query in case any other problem occurs (<a
href="http://dev.mysql.com/doc/refman/5.0/en/insert.html" class="broken_link" rel="nofollow">see the manual</a>). Thus it should be used if previously tested without the IGNORE keyword.</p><p>There is one more option: to use <a
href="http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html" class="broken_link" rel="nofollow">INSERT &#8230; ON DUPLICATE KEY UPDATE syntax</a>, and in the UPDATE part just <del
datetime="2008-08-30T14:13:11+00:00">do nothing</del> do some meaningless (empty) operation, like calculating 0+0 (<a
href="http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html#comment-80275">Geoffray</a> <ins
datetime="2008-08-30T14:13:11+00:00">suggests doing the id=id assignment for the MySQL optimization engine to ignore this operation</ins>). Advantage of this method is that it only ignores duplicate key events, and still aborts on other errors.</p><p>As a final notice: this post was inspired by <a
href="http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/">Xaprb</a>. I&#8217;d also advise to consult his other <a
href="http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/">post on writing flexible SQL queries</a>.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F10%2F18%2Fmysql-insert-if-not-exists-syntax.html&amp;linkname=MySQL%3A%20INSERT%20IF%20NOT%20EXISTS%20syntax" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F10%2F18%2Fmysql-insert-if-not-exists-syntax.html&amp;linkname=MySQL%3A%20INSERT%20IF%20NOT%20EXISTS%20syntax" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F10%2F18%2Fmysql-insert-if-not-exists-syntax.html&amp;linkname=MySQL%3A%20INSERT%20IF%20NOT%20EXISTS%20syntax" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F10%2F18%2Fmysql-insert-if-not-exists-syntax.html&amp;linkname=MySQL%3A%20INSERT%20IF%20NOT%20EXISTS%20syntax" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F10%2F18%2Fmysql-insert-if-not-exists-syntax.html&amp;linkname=MySQL%3A%20INSERT%20IF%20NOT%20EXISTS%20syntax" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F10%2F18%2Fmysql-insert-if-not-exists-syntax.html&#038;title=MySQL%3A%20INSERT%20IF%20NOT%20EXISTS%20syntax" data-a2a-url="https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html" data-a2a-title="MySQL: INSERT IF NOT EXISTS syntax"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html/feed</wfw:commentRss> <slash:comments>46</slash:comments> </item> </channel> </rss>