Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>use <code>INSERT IGNORE INTO table</code></p> <p>see <a href="http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html" rel="noreferrer">http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html</a></p> <p>there's also <code>INSERT … ON DUPLICATE KEY UPDATE</code> syntax, you can find explanations on <a href="http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html" rel="noreferrer">dev.mysql.com</a></p> <hr> <p><strong>Post from bogdan.org.ua according to <a href="http://webcache.googleusercontent.com/search?q=cache:bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html" rel="noreferrer">Google's webcache</a>:</strong></p> <blockquote> <p>18th October 2007</p> <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 … ON DUPLICATE KEY UPDATE.</p> <p>Imagine we have a table:</p> <pre><code>CREATE TABLE `transcripts` ( `ensembl_transcript_id` varchar(20) NOT NULL, `transcript_chrom_start` int(10) unsigned NOT NULL, `transcript_chrom_end` int(10) unsigned NOT NULL, PRIMARY KEY (`ensembl_transcript_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; </code></pre> <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 ‘duplicate primary key’ errors.</p> <p>Method 1: using REPLACE</p> <p>It’s very simple:</p> <pre><code>REPLACE INTO `transcripts` SET `ensembl_transcript_id` = ‘ENSORGT00000000001′, `transcript_chrom_start` = 12345, `transcript_chrom_end` = 12678; </code></pre> <p>If the record exists, it will be overwritten; if it does not yet exist, it will be created. However, using this method isn’t efficient for our case: we do not need to overwrite existing records, it’s fine just to skip them.</p> <p>Method 2: using INSERT IGNORE Also very simple:</p> <pre><code>INSERT IGNORE INTO `transcripts` SET `ensembl_transcript_id` = ‘ENSORGT00000000001′, `transcript_chrom_start` = 12345, `transcript_chrom_end` = 12678; </code></pre> <p>Here, if the ‘ensembl_transcript_id’ is already present in the database, it will be silently skipped (ignored). (To be more precise, here’s a quote from MySQL reference manual: “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.”.) If the record doesn’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 (see the manual). Thus it should be used if previously tested without the IGNORE keyword.</p> <p>There is one more option: to use INSERT … ON DUPLICATE KEY UPDATE syntax, and in the UPDATE part just do nothing do some meaningless (empty) operation, like calculating 0+0 (Geoffray suggests doing the id=id assignment for the MySQL optimization engine to ignore this operation). 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 Xaprb. I’d also advise to consult his other post on writing flexible SQL queries.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload