<?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; database</title> <atom:link href="https://bogdan.org.ua/tags/database/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>Weird orthology species names in Ensembl</title><link>https://bogdan.org.ua/2008/09/30/weird-orthology-species-names-in-ensembl.html</link> <comments>https://bogdan.org.ua/2008/09/30/weird-orthology-species-names-in-ensembl.html#comments</comments> <pubDate>Tue, 30 Sep 2008 11:01:59 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Bioinformatics]]></category> <category><![CDATA[Science]]></category> <category><![CDATA[compara]]></category> <category><![CDATA[convention]]></category> <category><![CDATA[cow]]></category> <category><![CDATA[database]]></category> <category><![CDATA[Ensembl]]></category> <category><![CDATA[non-unique]]></category> <category><![CDATA[orthology]]></category> <category><![CDATA[release]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=398</guid> <description><![CDATA[For the COTRASIF tool, I&#8217;ve been using the Ensembl Compara database (since release 47) to automatically import into COTRASIF gene orthology mappings. However, with the E!50 release, the Compara database was dropped. Looking for another option to get orthologs from Ensembl (using martservice, via biomart.org), I tried using the standard query &#8211; selecting &#8220;Homologs&#8221; group [&#8230;]]]></description> <content:encoded><![CDATA[<p>For the <a
href="http://biomed.org.ua/COTRASIF/" class="broken_link" rel="nofollow">COTRASIF tool</a>, I&#8217;ve been using the Ensembl Compara database (since release 47) to automatically import into COTRASIF gene orthology mappings.</p><p>However, with the E!50 release, the Compara database was dropped.</p><p>Looking for another option to get orthologs from Ensembl (using martservice, via <a
href="http://www.biomart.org/">biomart.org</a>), I tried using the standard query &#8211; selecting &#8220;Homologs&#8221; group on the &#8220;Attributes&#8221; page for a single species database, and then selecting appropriate second species to get orthology mappings.</p><p>Imagine my surprise, when not only in the interface, but also in the generated XML file I found attribute names like &#8220;cow_ensembl_gene&#8221; :-O</p><p>I only need 11 species at the moment, and excluding the sufficiently unique name mappings like &#8220;zebrafish &#8211; danio rerio&#8221;, there is a number of questionable mappings: &#8220;yeast&#8221; for <em>S. cerevisiae</em> (could be <em>S.pombe</em>), &#8220;rat&#8221; for <em>R. norvegicus</em> (could be <em>R.rattus</em>), &#8220;anopheles&#8221; for <em>A.gambiae</em> (could be some other <em>Anopheles</em>). Other mappings might be also non-unique, especially for people working with different species of the same genus.</p><p>Am I missing some system in this naming &#8220;convention&#8221;, or am I the only one who finds it strange?</p><p>Is there a way <em>not</em> to use &#8220;common species names&#8221; when importing orthology data from Ensembl with the help of martservice?</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F09%2F30%2Fweird-orthology-species-names-in-ensembl.html&amp;linkname=Weird%20orthology%20species%20names%20in%20Ensembl" 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%2F2008%2F09%2F30%2Fweird-orthology-species-names-in-ensembl.html&amp;linkname=Weird%20orthology%20species%20names%20in%20Ensembl" 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%2F2008%2F09%2F30%2Fweird-orthology-species-names-in-ensembl.html&amp;linkname=Weird%20orthology%20species%20names%20in%20Ensembl" 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%2F2008%2F09%2F30%2Fweird-orthology-species-names-in-ensembl.html&amp;linkname=Weird%20orthology%20species%20names%20in%20Ensembl" 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%2F2008%2F09%2F30%2Fweird-orthology-species-names-in-ensembl.html&amp;linkname=Weird%20orthology%20species%20names%20in%20Ensembl" 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%2F2008%2F09%2F30%2Fweird-orthology-species-names-in-ensembl.html&#038;title=Weird%20orthology%20species%20names%20in%20Ensembl" data-a2a-url="https://bogdan.org.ua/2008/09/30/weird-orthology-species-names-in-ensembl.html" data-a2a-title="Weird orthology species names in Ensembl"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2008/09/30/weird-orthology-species-names-in-ensembl.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Convert MySQL database from one encoding/collation into another</title><link>https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html</link> <comments>https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html#comments</comments> <pubDate>Fri, 08 Feb 2008 19:17:45 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Notepad]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Web]]></category> <category><![CDATA[collation]]></category> <category><![CDATA[convert]]></category> <category><![CDATA[database]]></category> <category><![CDATA[encoding]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[latin1]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[script]]></category> <category><![CDATA[utf8]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html</guid> <description><![CDATA[Most frequent use: convert database from latin1_swedish to utf8_general_ci. Original script found at: MySQL and UTF-8. Update: the original script had an error, it would generate queries likes this one (note the bold part): ALTER TABLE `links` CHANGE `link_rel` `link_rel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT NULL; This is clearly wrong syntax [&#8230;]]]></description> <content:encoded><![CDATA[<p>Most frequent use: <a
title="self-link" href="http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html">convert database from latin1_swedish to utf8_general_ci</a>.<br
/> Original script found at: <a
href="http://www.phpwact.org/php/i18n/utf-8/mysql" class="broken_link" rel="nofollow">MySQL and UTF-8</a>.</p><p><ins
datetime="2008-02-22T21:50:28+00:00">Update:</ins> the original script <u>had an error</u>, it would generate queries likes this one (note the bold part):</p><blockquote><p>ALTER TABLE `links` CHANGE `link_rel` `link_rel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci <strong>NOT NULL  DEFAULT NULL</strong>;</p></blockquote><p>This is clearly wrong syntax (and logic). I fixed this by making comparison to NULL strict (with three equal signs instead of two):</p><blockquote><p> // Does the field default to null, a string, or nothing?<br
/> if ($row['Default'] === NULL)</p></blockquote><p><ins
datetime="2008-06-27T07:23:50+00:00">Update 2:</ins> based on comment by <em>banesto</em>, I modified the script; now it does not require specifying the from_collation, it&#8217;s sufficient to specify to_collation (which will be used for all the fields and tables). The modified code is:</p><blockquote><p>if ($row['Collation'] == &#8221; || $row['Collation'] == $convert_to)<br
/> continue;</p></blockquote><p><ins
datetime="2010-06-28T11:58:11+00:00">Update 3:</ins> the long-lasting, re-appearing NOT NULL DEFAULT NULL problem is finally fixed.</p><p><ins
datetime="2010-06-28T12:11:38+00:00">Update 4:</ins> incorporated Russ&#8217;s fix to skip numeric fields (in order to leave autoincrement values intact).</p><p>Here&#8217;s the script itself: (to copy-paste: first click the &#8220;Plain text&#8221; header)<br
/> <span
id="more-255"></span></p><div
id="ig-sh-1" class="syntax_hilite"><div
class="code"><ol
class="php" 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: #000000; font-weight: bold;">&lt;?php</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: #666666; font-style: italic;">// Original script (v1.0) by/from: http://www.phpwact.org/php/i18n/utf-8/mysql</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: #666666; font-style: italic;">// Improved/modified (v1.05) by Bogdan http://bogdan.org.ua/</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: #666666; font-style: italic;">// Last updated: 2010-06-28</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;</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: #666666; font-style: italic;">// this script will output all queries needed to change all fields/tables to a different collation</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: #666666; font-style: italic;">// it is HIGHLY suggested you make a MySQL backup prior to running any of the generated queries</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;</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: #666666; font-style: italic;">// this code is provided AS IS and without any warranty</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;</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: #666666; font-style: italic;">// add text/plain header when used not as a CLI script; PHP CLI SAPI shouldn't output headers</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: #990000;">header</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot;Content-Type: text/plain&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</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;</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: #666666; font-style: italic;">// precaution</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: #990000;">die</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot;Make a backup of your MySQL database, then remove this line from the code!&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</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;</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: #990000;">set_time_limit</span><span
style="color: #009900;">&#40;</span><span
style="color: #cc66cc;">0</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</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;</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: #666666; font-style: italic;">// collation you want to change to:</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: #000088;">$convert_to</span> &nbsp; <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'utf8_general_ci'</span><span
style="color: #339933;">;</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;</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: #666666; font-style: italic;">// character set of new collation:</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: #000088;">$character_set</span><span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'utf8'</span><span
style="color: #339933;">;</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;</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: #666666; font-style: italic;">// DB login information - *modify before use*</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: #000088;">$username</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'user'</span><span
style="color: #339933;">;</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: #000088;">$password</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'pass'</span><span
style="color: #339933;">;</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: #000088;">$database</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'database_name'</span><span
style="color: #339933;">;</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: #000088;">$host</span> &nbsp; &nbsp; <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'localhost'</span><span
style="color: #339933;">;</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;</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: #666666; font-style: italic;">//-- usually, there is nothing to modify below this line --//</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;</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: #666666; font-style: italic;">// show TABLE alteration queries?</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: #000088;">$show_alter_table</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">true</span><span
style="color: #339933;">;</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: #666666; font-style: italic;">// show FIELD alteration queries?</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: #000088;">$show_alter_field</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">true</span><span
style="color: #339933;">;</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;</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: #990000;">mysql_connect</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$host</span><span
style="color: #339933;">,</span> <span
style="color: #000088;">$username</span><span
style="color: #339933;">,</span> <span
style="color: #000088;">$password</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</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: #990000;">mysql_select_db</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$database</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</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;</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: #000088;">$rs_tables</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_query</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot; SHOW TABLES &quot;</span><span
style="color: #009900;">&#41;</span> or <span
style="color: #990000;">die</span><span
style="color: #009900;">&#40;</span><span
style="color: #990000;">mysql_error</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</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;</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: #b1b100;">while</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row_tables</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_fetch_row</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$rs_tables</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</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; &nbsp; <span
style="color: #000088;">$table</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row_tables</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;0&amp;#93;);</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; &nbsp;</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; &nbsp; <span
style="color: #666666; font-style: italic;">// Alter table collation</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; &nbsp; <span
style="color: #666666; font-style: italic;">// ALTER TABLE `account` DEFAULT CHARACTER SET utf8</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; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$show_alter_table</span><span
style="color: #009900;">&#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;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">echo</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot;ALTER TABLE `<span
style="color: #006699; font-weight: bold;">$table</span>` DEFAULT CHARACTER SET <span
style="color: #006699; font-weight: bold;">$character_set</span>;<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</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;</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; &nbsp; <span
style="color: #000088;">$rs</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_query</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot; SHOW FULL FIELDS FROM `<span
style="color: #006699; font-weight: bold;">$table</span>` &quot;</span><span
style="color: #009900;">&#41;</span> or <span
style="color: #990000;">die</span><span
style="color: #009900;">&#40;</span><span
style="color: #990000;">mysql_error</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</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;</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; &nbsp; <span
style="color: #b1b100;">while</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_fetch_assoc</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$rs</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</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; &nbsp; &nbsp; &nbsp;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Collation'&amp;#93; == '' || $row&amp;#91;'Collation'&amp;#93; == $convert_to )</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #b1b100;">continue</span><span
style="color: #339933;">;</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;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Is the field allowed to be null?</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; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Null'&amp;#93; == 'YES' )</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$nullable</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">' NULL '</span><span
style="color: #339933;">;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">else</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$nullable</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">' NOT NULL '</span><span
style="color: #339933;">;</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;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Does the field default to null, a string, or nothing?</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; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93; === NULL &amp;&amp; $row&amp;#91;'Null'&amp;#93; == 'YES' )</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">&quot; DEFAULT NULL &quot;</span><span
style="color: #339933;">;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">elseif</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93; != '' )</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">&quot; DEFAULT '&quot;</span><span
style="color: #339933;">.</span><span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93;).&quot;'&quot;;</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; &nbsp; &nbsp; &nbsp;<span
style="color: #b1b100;">else</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">''</span><span
style="color: #339933;">;</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;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// sanity check and fix:</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; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$nullable</span> <span
style="color: #339933;">==</span> <span
style="color: #0000ff;">' NOT NULL '</span> <span
style="color: #339933;">&amp;&amp;</span> <span
style="color: #000088;">$default</span> <span
style="color: #339933;">==</span> <span
style="color: #0000ff;">' DEFAULT NULL '</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">''</span><span
style="color: #339933;">;</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">echo</span> <span
style="color: #0000ff;">&quot;/* Warning: wrong combination of 'default value' and 'NULL-flag' detected - and fixed! */<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span
style="color: #339933;">;</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">echo</span> <span
style="color: #0000ff;">&quot;/* Diagnostics: row&amp;#91;Null&amp;#93; = '<span
style="color: #006699; font-weight: bold;">$row</span>&amp;#91;Null&amp;#93;', row&amp;#91;Default&amp;#93; = &quot;</span> <span
style="color: #339933;">.</span> <span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93;) . &quot;, MySQL version: &quot; . mysql_get_server_info() . &quot; */\n&quot;;</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; &nbsp; &nbsp; &nbsp;<span
style="color: #009900;">&#125;</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;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Don't alter INT columns: no collations, and altering them drops autoincrement values</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; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #990000;">strpos</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Type'&amp;#93;, 'int') !== false) {</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$show_alter_field</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">False</span><span
style="color: #339933;">;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">else</span> <span
style="color: #009900;">&#123;</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$show_alter_field</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">True</span><span
style="color: #339933;">;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</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;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Alter field collation:</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; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// ALTER TABLE `tab` CHANGE `field` `field` CHAR( 5 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL</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; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$show_alter_field</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$field</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Field'&amp;#93;);</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #b1b100;">echo</span> <span
style="color: #0000ff;">&quot;ALTER TABLE `<span
style="color: #006699; font-weight: bold;">$table</span>` CHANGE `<span
style="color: #006699; font-weight: bold;">$field</span>` `<span
style="color: #006699; font-weight: bold;">$field</span>` <span
style="color: #006699; font-weight: bold;">$row</span>&amp;#91;Type&amp;#93; CHARACTER SET <span
style="color: #006699; font-weight: bold;">$character_set</span> COLLATE <span
style="color: #006699; font-weight: bold;">$convert_to</span> <span
style="color: #006699; font-weight: bold;">$nullable</span> <span
style="color: #006699; font-weight: bold;">$default</span>;<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span
style="color: #339933;">;</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; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</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; &nbsp; <span
style="color: #009900;">&#125;</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: #009900;">&#125;</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: #000000; font-weight: bold;">?&gt;</span></div></li></ol></div></div><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" 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%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" 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%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" 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%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" 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%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" 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%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&#038;title=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" data-a2a-url="https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html" data-a2a-title="Convert MySQL database from one encoding/collation into another"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html/feed</wfw:commentRss> <slash:comments>60</slash:comments> </item> <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-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;">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-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;">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-4" 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>