<?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 ON CONFLICT DO NOTHING &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/insert-on-conflict-do-nothing/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>Sun, 09 Apr 2017 20:18:10 +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: Multiple columns or keys in ON CONFLICT clause</title>
		<link>https://www.dbrnd.com/2017/04/postgresql-9-5-multiple-columns-or-keys-in-on-conflict-clause/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 17 Apr 2017 16:16:35 +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 ON CONFLICT DO NOTHING]]></category>
		<category><![CDATA[INSERT ON CONFLICT DO UPDATE]]></category>
		<category><![CDATA[ON CONFLICT]]></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 Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5038</guid>

					<description><![CDATA[I already a shared article on ON CONFLICT clause of PostgreSQL 9.5 PostgreSQL 9.5: Insert IF not Exists, Update IF Exists (Insert ON CONFLICT option) In above article, I used only one single key in ON CONFLICT clause. A day before yesterday, I got an email like, does it require to add a unique index [&#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%2F2017%2F04%2Fpostgresql-9-5-multiple-columns-or-keys-in-on-conflict-clause%2F&amp;linkname=PostgreSQL%209.5%3A%20Multiple%20columns%20or%20keys%20in%20ON%20CONFLICT%20clause" 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%2F2017%2F04%2Fpostgresql-9-5-multiple-columns-or-keys-in-on-conflict-clause%2F&amp;linkname=PostgreSQL%209.5%3A%20Multiple%20columns%20or%20keys%20in%20ON%20CONFLICT%20clause" 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%2F2017%2F04%2Fpostgresql-9-5-multiple-columns-or-keys-in-on-conflict-clause%2F&amp;linkname=PostgreSQL%209.5%3A%20Multiple%20columns%20or%20keys%20in%20ON%20CONFLICT%20clause" 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%2F2017%2F04%2Fpostgresql-9-5-multiple-columns-or-keys-in-on-conflict-clause%2F&amp;linkname=PostgreSQL%209.5%3A%20Multiple%20columns%20or%20keys%20in%20ON%20CONFLICT%20clause" 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%2F2017%2F04%2Fpostgresql-9-5-multiple-columns-or-keys-in-on-conflict-clause%2F&amp;linkname=PostgreSQL%209.5%3A%20Multiple%20columns%20or%20keys%20in%20ON%20CONFLICT%20clause" 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%2F2017%2F04%2Fpostgresql-9-5-multiple-columns-or-keys-in-on-conflict-clause%2F&amp;linkname=PostgreSQL%209.5%3A%20Multiple%20columns%20or%20keys%20in%20ON%20CONFLICT%20clause" 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%2F2017%2F04%2Fpostgresql-9-5-multiple-columns-or-keys-in-on-conflict-clause%2F&#038;title=PostgreSQL%209.5%3A%20Multiple%20columns%20or%20keys%20in%20ON%20CONFLICT%20clause" data-a2a-url="https://www.dbrnd.com/2017/04/postgresql-9-5-multiple-columns-or-keys-in-on-conflict-clause/" data-a2a-title="PostgreSQL 9.5: Multiple columns or keys in ON CONFLICT clause"></a></p><p>I already a shared article on ON CONFLICT clause of PostgreSQL 9.5</p>
<blockquote class="wp-embedded-content" data-secret="pgs1goO9zm"><p><a href="https://www.dbrnd.com/2016/08/postgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing/">PostgreSQL 9.5: Insert IF not Exists, Update IF Exists (Insert ON CONFLICT option)</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/08/postgresql-9-5-insert-if-not-exists-update-if-exists-insert-on-conflict-do-update-do-nothing/embed/#?secret=pgs1goO9zm" data-secret="pgs1goO9zm" width="600" height="338" title="&#8220;PostgreSQL 9.5: Insert IF not Exists, Update IF Exists (Insert ON CONFLICT option)&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe><br />
In above article, I used only one single key in ON CONFLICT clause. </p>
<blockquote><p>A day before yesterday, I got an email like, does it require to add a unique index on those columns which we require to be in ON CONFLICT clause?<br />
<strong>The answer is: Yes</strong></p></blockquote>
<p>You must need to define a unique index on those columns which you are planning to use in ON CONFLICT clause because it can only check the duplicates bases on unique indexes only. </p>
<p><strong>Below is a demonstration of this:</strong></p>
<p><strong>Create a table with sample data with composite PRIMARY KEY:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Employee
(	
	EmpID INT 
	,EmpName CHARACTER VARYING 
	,CONSTRAINT pk_tbl_Employee_EmpID_EmpName PRIMARY KEY (EmpID,EmpName)
);
 
INSERT INTO tbl_Employee 
VALUES (1,'Anvesh'),(2,'Roy'),(3,'Lee')
,(4,'Nivu'),(5,'Rajesh'),(6,'Nupur');</pre> </p>
<p><strong>Try to insert a duplicate EmpID record, using option INSERT ON DO NOTHING:</strong><br />
It will insert a new record because key EmpID + EmpName is not duplicate. But if you execute the same, insert the second time, it will do nothing.</p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Employee 
VALUES (6,'Noor')
ON CONFLICT (EmpID,EmpName)
DO NOTHING;</pre> </p>
<p><strong>Check the result:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM tbl_Employee;

/*
EmpID   EmpName
-----------------
1       Anvesh
2       Roy
3       Lee
4       Nivu
5       Rajesh
6       Nupur
6       Noor
*/</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<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>
	</channel>
</rss>
