<?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>insert if not exists &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/insert-if-not-exists/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Wed, 13 Sep 2017 11:51:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL 9.5: Insert IF not Exists, Update IF Exists (Insert ON CONFLICT option)</title>
		<link>https://www.dbrnd.com/2016/08/postgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing/</link>
					<comments>https://www.dbrnd.com/2016/08/postgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 15 Aug 2016 14:42:27 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[insert if not exists]]></category>
		<category><![CDATA[INSERT ON CONFLICT DO NOTHING]]></category>
		<category><![CDATA[INSERT ON CONFLICT DO UPDATE]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[postgresql 9.5]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[UPDATE IF EXISTS]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3449</guid>

					<description><![CDATA[After a long time of waiting, PostgreSQL 9.5 introduced INSERT ON CONFLICT [DO UPDATE] [DO NOTHING]. This option basically helps to perform DML actions like, Insert IF not Exists, Update IF Exists. Previously, we have to use upsert or merge statement to do this kind of operation. I have also published an article on it. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing%2F&amp;linkname=PostgreSQL%209.5%3A%20Insert%20IF%20not%20Exists%2C%20Update%20IF%20Exists%20%28Insert%20ON%20CONFLICT%20option%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing%2F&amp;linkname=PostgreSQL%209.5%3A%20Insert%20IF%20not%20Exists%2C%20Update%20IF%20Exists%20%28Insert%20ON%20CONFLICT%20option%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing%2F&amp;linkname=PostgreSQL%209.5%3A%20Insert%20IF%20not%20Exists%2C%20Update%20IF%20Exists%20%28Insert%20ON%20CONFLICT%20option%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing%2F&amp;linkname=PostgreSQL%209.5%3A%20Insert%20IF%20not%20Exists%2C%20Update%20IF%20Exists%20%28Insert%20ON%20CONFLICT%20option%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing%2F&amp;linkname=PostgreSQL%209.5%3A%20Insert%20IF%20not%20Exists%2C%20Update%20IF%20Exists%20%28Insert%20ON%20CONFLICT%20option%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing%2F&amp;linkname=PostgreSQL%209.5%3A%20Insert%20IF%20not%20Exists%2C%20Update%20IF%20Exists%20%28Insert%20ON%20CONFLICT%20option%29" title="Pocket" 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%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing%2F&#038;title=PostgreSQL%209.5%3A%20Insert%20IF%20not%20Exists%2C%20Update%20IF%20Exists%20%28Insert%20ON%20CONFLICT%20option%29" data-a2a-url="https://www.dbrnd.com/2016/08/postgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing/" data-a2a-title="PostgreSQL 9.5: Insert IF not Exists, Update IF Exists (Insert ON CONFLICT option)"></a></p><p>After a long time of waiting, PostgreSQL 9.5 introduced INSERT ON CONFLICT [DO UPDATE] [DO NOTHING].</p>
<blockquote><p>This option basically helps to perform DML actions like, Insert IF not Exists, Update IF Exists.</p></blockquote>
<p>Previously, we have to use upsert or merge statement to do this kind of operation. I have also published an article on it.</p>
<blockquote data-secret="0071cyB9q5" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2015/10/postgresql-insert-update-or-upsert-merge-using-writable-cte/">PostgreSQL: Insert &#8211; Update or Upsert &#8211; Merge using writable CTE</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/10/postgresql-insert-update-or-upsert-merge-using-writable-cte/embed/#?secret=0071cyB9q5" data-secret="0071cyB9q5" width="600" height="338" title="&#8220;PostgreSQL: Insert &#8211; Update or Upsert &#8211; Merge using writable CTE&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>This newly option has two varieties:</strong></p>
<ul>
<li><strong>INSERT ON CONFLICT DO UPDATE: </strong>If record matched, it is updated with the new data value.</li>
<li><strong>INSERT ON CONFLICT DO NOTHING:</strong> If record matched, it skips the record or error.</li>
</ul>
<p><strong>Below is a full demonstration of this:</strong></p>
<p><strong>Create a table with sample data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_Employee
(	
	EmpID INT PRIMARY KEY
	,EmpName CHARACTER VARYING 
);

INSERT INTO tbl_Employee 
VALUES (1,'Anvesh'),(2,'Roy'),(3,'Lee')
,(4,'Nivu'),(5,'Rajesh'),(6,'Nupur');</pre><p>
<strong>Insert one more row with option INSERT ON CONFLICT DO UPDATE:</strong></p>
<p>Using this option, if a conflict occurs then it will update the mentioned data.<br />
Here, I have use &#8220;Excluded&#8221; table which is special table and contains the row-to-be-inserted.<br />
<pre class="crayon-plain-tag">INSERT INTO tbl_Employee 
VALUES (7,'Ramu')
ON CONFLICT (EmpID)
DO UPDATE SET EmpName = Excluded.EmpName;</pre>
<strong>Try to update last inserted EmpName, using option INSERT ON CONFLICT DO UPDATE:</strong><br />
<pre class="crayon-plain-tag">INSERT INTO tbl_Employet 
VALUES (7,'Mahi')
ON CONFLICT (EmpID)
DO UPDATE SET EmpName = Excluded.EmpName;</pre>
<strong>Try to insert duplicate EmpID record, using option INSERT ON DO NOTHING:</strong><br />
Using this option, if conflict occurs then it will not take any action or any error.<br />
<pre class="crayon-plain-tag">INSERT INTO tbl_Employee 
VALUES (8,'Noor')
ON CONFLICT (EmpID)
DO NOTHING;

INSERT INTO tbl_Employee 
VALUES (8,'Noor')
ON CONFLICT (EmpID)
DO NOTHING;</pre>
<strong>Check the result:</strong><br />
<pre class="crayon-plain-tag">SELECT *FROM tbl_Employee;

/*
EmpID   EmpName
-----------------
1       Anvesh
2       Roy
3       Lee
4       Nivu
5       Rajesh
6       Nupur
7       Mahi
8       Noor
*/</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/08/postgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>How to Insert if not exists in MySQL</title>
		<link>https://www.dbrnd.com/2015/09/insert-ignore-if-not-exists-in-mysql/</link>
					<comments>https://www.dbrnd.com/2015/09/insert-ignore-if-not-exists-in-mysql/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 02 Sep 2015 16:54:50 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[insert if not exists]]></category>
		<category><![CDATA[insert ignore]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=471</guid>

					<description><![CDATA[In this post, I am sharing a demonstration on how to skip the INSERT statement for duplicate records in MySQL. Insert if not exists: Basically, there are two situations for bulk insertion: First: during insertion, if the record exists, update the old value with new the value. This I have already posted so please visit [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Finsert-ignore-if-not-exists-in-mysql%2F&amp;linkname=How%20to%20Insert%20if%20not%20exists%20in%20MySQL" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Finsert-ignore-if-not-exists-in-mysql%2F&amp;linkname=How%20to%20Insert%20if%20not%20exists%20in%20MySQL" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Finsert-ignore-if-not-exists-in-mysql%2F&amp;linkname=How%20to%20Insert%20if%20not%20exists%20in%20MySQL" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Finsert-ignore-if-not-exists-in-mysql%2F&amp;linkname=How%20to%20Insert%20if%20not%20exists%20in%20MySQL" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Finsert-ignore-if-not-exists-in-mysql%2F&amp;linkname=How%20to%20Insert%20if%20not%20exists%20in%20MySQL" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Finsert-ignore-if-not-exists-in-mysql%2F&amp;linkname=How%20to%20Insert%20if%20not%20exists%20in%20MySQL" title="Pocket" 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%2Fwww.dbrnd.com%2F2015%2F09%2Finsert-ignore-if-not-exists-in-mysql%2F&#038;title=How%20to%20Insert%20if%20not%20exists%20in%20MySQL" data-a2a-url="https://www.dbrnd.com/2015/09/insert-ignore-if-not-exists-in-mysql/" data-a2a-title="How to Insert if not exists in MySQL"></a></p><p>In this post, I am sharing a demonstration on how to skip the INSERT statement for duplicate records in MySQL.</p>
<blockquote><p>Insert if not exists:</p></blockquote>
<p><strong>Basically, there are two situations for bulk insertion:</strong></p>
<p>First: during insertion, if the record exists, update the old value with new the value.</p>
<p>This I have already posted so please visit this page.<br />
href=&#8221;https://www.dbrnd.com/2015/08/insert-update-merge-statement-in-mysql</p>
<p>Second: during insertion, if the record exists, skip the new value for an insert. This I am going to explain in this post.</p>
<p><strong>MySQL INSERT IGNORE statement:</strong></p>
<blockquote><p>Using this, you can perform actions like, &#8220;Insert if not exists&#8221;.</p>
<p>First, create primary data by applying proper composite unique key or primary key.<br />
If the same key is trying to INSERT during the new insertion, INSERT IGNORE Statement only skip that transaction.</p></blockquote>
<p><strong>Below is a full demonstration:</strong><br />
I created two tables and transferring some duplicate data from one to another using INSERT IGNORE statement.</p>
<p><strong>Let&#8217;s first create tables and sample data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_First
(
   ID INTEGER 
   ,Name VARCHAR(255)
);

INSERT INTO tbl_First VALUES
(1,'ABC'),(1,'LKM')
,(3,'PQR'),(4,'XYZ');

CREATE TABLE tbl_Second
(
   ID INTEGER PRIMARY KEY
   ,Name VARCHAR(255)
);</pre><p>
<p>We created two tables, table first has no primary key and table second has a primary key.</p>
<p>You can find the duplicate key in the insert statement.</p>
<p><strong>Now, first try to copy this data from the table first to the second table:</strong></p><pre class="crayon-plain-tag">INSERT INTO tbl_Second 
SELECT *FROM tbl_First;</pre><p>
<strong>After executing this, You will get an error for duplicate keys.</strong></p>
<p><strong>Now, apply INSERT IGNORE Statement:</strong></p><pre class="crayon-plain-tag">INSERT IGNORE INTO tbl_Second
SELECT *FROM tbl_First;</pre><p>
</p><pre class="crayon-plain-tag">SELECT *FROM tbl_Second;</pre><p>
<strong>Result is :</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLInsertIgnore.png"><img decoding="async" class="alignnone wp-image-476 size-full" title="MySQL Insert Ignore Statement" src="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLInsertIgnore.png" alt="MySQL Insert Ignore Statement" width="110" height="96" /></a></p>
<p>You can see the result where duplicate key ignored by the insert statement.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/09/insert-ignore-if-not-exists-in-mysql/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
