<?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>ON DUPLICATE KEY UPDATE &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/on-duplicate-key-update/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>Tue, 21 Nov 2017 06:14:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.5</generator>
	<item>
		<title>Insert-Update, Merge statement in MySQL</title>
		<link>https://www.dbrnd.com/2015/08/insert-update-merge-statement-in-mysql/</link>
					<comments>https://www.dbrnd.com/2015/08/insert-update-merge-statement-in-mysql/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 18 Aug 2015 16:14:04 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></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>
		<category><![CDATA[ON DUPLICATE KEY UPDATE]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=426</guid>

					<description><![CDATA[In this post, I am sharing a demonstration on the merge statement for Insert-Update in MySQL. For all the database developers merge statement is very popular to handle insert, update and delete into a single statement. Microsoft SQL Server provides the merge statement in which you can manage Insert, update and delete and can make [&#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%2F08%2Finsert-update-merge-statement-in-mysql%2F&amp;linkname=Insert-Update%2C%20Merge%20statement%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%2F08%2Finsert-update-merge-statement-in-mysql%2F&amp;linkname=Insert-Update%2C%20Merge%20statement%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%2F08%2Finsert-update-merge-statement-in-mysql%2F&amp;linkname=Insert-Update%2C%20Merge%20statement%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%2F08%2Finsert-update-merge-statement-in-mysql%2F&amp;linkname=Insert-Update%2C%20Merge%20statement%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%2F08%2Finsert-update-merge-statement-in-mysql%2F&amp;linkname=Insert-Update%2C%20Merge%20statement%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%2F08%2Finsert-update-merge-statement-in-mysql%2F&amp;linkname=Insert-Update%2C%20Merge%20statement%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%2F08%2Finsert-update-merge-statement-in-mysql%2F&#038;title=Insert-Update%2C%20Merge%20statement%20in%20MySQL" data-a2a-url="https://www.dbrnd.com/2015/08/insert-update-merge-statement-in-mysql/" data-a2a-title="Insert-Update, Merge statement in MySQL"></a></p><p>In this post, I am sharing a demonstration on the merge statement for Insert-Update in MySQL.</p>
<p>For all the database developers merge statement is very popular to handle insert, update and delete into a single statement.</p>
<p>Microsoft SQL Server provides the merge statement in which you can manage Insert, update and delete and can make a single statement. </p>
<p>But unfortunately, MySQL server has no any default Merge Statement.<br />
It has a similar alternative option for merging of Insert, Update. </p>
<p>You can use <strong>INSERT ON DUPLICATE KEY UPDATE</strong> in MySQL.</p>
<p><strong>Let&#8217;s first create sample table with data:</strong></p>
<pre class="crayon-plain-tag">CREATE DATABASE Employee;
CREATE TABLE Employee.tbl_TestMerge
(
    EmpNumber INTEGER PRIMARY KEY,
    EmpName VARCHAR(50)	
);
INSERT INTO Employee.tbl_TestMerge
VALUES (1,'ABC'),(2,'XYZ'),(3,'PQR')
,(4,'EFG'),(5,'JKL');</pre>
<p><strong>Use INSERT ON DUPLICATE KEY UPDATE:</strong></p><pre class="crayon-plain-tag">INSERT INTO Employee.tbl_TestMerge 
(EmpNumber,EmpName)
VALUES (2,'LKM')
ON DUPLICATE KEY UPDATE 
EmpNumber=VALUES(EmpNumber),EmpName=VALUES(EmpName);</pre><p>
<p>In the above code, you can see EmpNumber is Primary Key and I am trying to insert same EmpNumber=2 with different EmpName.</p>
<p>As you can check that, I also mentioned ON DUPLICATE KEY UPDATE with Insert Statement.</p>
<p>Now, this statement first checks the value of data and if data with the same key, internally execute update statement otherwise it executes the insert statement.</p>
<p><strong>The Result:</strong><br />
<a href="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLMerge.png"><img decoding="async" class="alignnone wp-image-436 size-full" title="MySQL Merge Statement " src="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLMerge.png" alt="MySQLMerge" width="216" height="213" srcset="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLMerge.png 216w, https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLMerge-150x148.png 150w" sizes="(max-width: 216px) 100vw, 216px" /></a></p>
<p>This way you can use ON DUPLICATE KEY UPDATE statement for merging Insert and Update statement.</p>
<blockquote><p><strong>Important points:</strong><br />
If you are using ON DUPLICATE KEY UPDATE statement, your table has must Primary key or at least one Unique Key.</p>
<p>If you are using Auto_Increment number, your LAST_INSERT_ID() function will not work properly because if data updates, also Auto_Increment ID will increase a number internally. This is a disadvantage of this statement.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/08/insert-update-merge-statement-in-mysql/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
	</channel>
</rss>
