{"id":12629,"date":"2024-03-14T16:14:09","date_gmt":"2024-03-14T16:14:09","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/"},"modified":"2025-08-05T04:29:14","modified_gmt":"2025-08-05T04:29:14","slug":"how-is-the-merge-statement-used-in-mysql","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/","title":{"rendered":"MySQL MERGE Alternative: INSERT ON DUPLICATE"},"content":{"rendered":"<p>There is no MERGE statement in MySQL, but similar functionality can be achieved using other methods.<\/p>\n<p>In MySQL, you can achieve similar functionality to a MERGE statement by using the INSERT INTO &#8230; ON DUPLICATE KEY UPDATE statement. The syntax for this statement is as follows:<\/p>\n<pre class=\"post-pre\"><code>INSERT INTO table_name (column1, column2, ...)\r\nVALUES (value1, value2, ...)\r\nON DUPLICATE KEY UPDATE column1 = value1, column2 = value2, ...\r\n<\/code><\/pre>\n<p>This statement is used to insert data into a table, and if there is a duplicate unique key, the corresponding column will be updated. By specifying the columns to be updated and their values in the ON DUPLICATE KEY UPDATE clause, the update operation is performed.<\/p>\n<p>For instance, let&#8217;s say there is a table called employees, with three columns: id, name, and salary, where id is the primary key. If inserting a new record, and there is already a record with the same id, then update the corresponding name and salary columns using the following statement:<\/p>\n<pre class=\"post-pre\"><code>INSERT INTO employees (id, name, salary)\r\nVALUES (1, 'John Doe', 5000)\r\nON DUPLICATE KEY UPDATE name = 'John Doe', salary = 5000;\r\n<\/code><\/pre>\n<p>If a record with id 1 already exists, the name and salary columns will be updated; otherwise, a new record will be inserted.<\/p>\n<p>It should be noted that the MySQL INSERT INTO&#8230; ON DUPLICATE KEY UPDATE statement can only be used for tables with a unique key. If the table does not have a unique key, a similar functionality can be achieved by creating a unique index.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is no MERGE statement in MySQL, but similar functionality can be achieved using other methods. In MySQL, you can achieve similar functionality to a MERGE statement by using the INSERT INTO &#8230; ON DUPLICATE KEY UPDATE statement. The syntax for this statement is as follows: INSERT INTO table_name (column1, column2, &#8230;) VALUES (value1, value2, [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[16588,16586,796,16589,16587],"class_list":["post-12629","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-insert-on-duplicate","tag-mysql-merge","tag-mysql-tutorial","tag-sql-merge-alternative","tag-upsert-mysql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MySQL MERGE Alternative: INSERT ON DUPLICATE - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to perform UPSERT operations in MySQL without MERGE - use INSERT ON DUPLICATE KEY UPDATE syntax explained.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL MERGE Alternative: INSERT ON DUPLICATE\" \/>\n<meta property=\"og:description\" content=\"Learn how to perform UPSERT operations in MySQL without MERGE - use INSERT ON DUPLICATE KEY UPDATE syntax explained.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-14T16:14:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-05T04:29:14+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin Taylor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Taylor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/\"},\"author\":{\"name\":\"Benjamin Taylor\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\"},\"headline\":\"MySQL MERGE Alternative: INSERT ON DUPLICATE\",\"datePublished\":\"2024-03-14T16:14:09+00:00\",\"dateModified\":\"2025-08-05T04:29:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/\"},\"wordCount\":214,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"INSERT ON DUPLICATE\",\"MySQL MERGE\",\"MySQL tutorial\",\"SQL MERGE Alternative\",\"UPSERT MySQL\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/\",\"name\":\"MySQL MERGE Alternative: INSERT ON DUPLICATE - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-14T16:14:09+00:00\",\"dateModified\":\"2025-08-05T04:29:14+00:00\",\"description\":\"Learn how to perform UPSERT operations in MySQL without MERGE - use INSERT ON DUPLICATE KEY UPDATE syntax explained.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL MERGE Alternative: INSERT ON DUPLICATE\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\",\"name\":\"Benjamin Taylor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"caption\":\"Benjamin Taylor\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"MySQL MERGE Alternative: INSERT ON DUPLICATE - Blog - Silicon Cloud","description":"Learn how to perform UPSERT operations in MySQL without MERGE - use INSERT ON DUPLICATE KEY UPDATE syntax explained.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/","og_locale":"en_US","og_type":"article","og_title":"MySQL MERGE Alternative: INSERT ON DUPLICATE","og_description":"Learn how to perform UPSERT operations in MySQL without MERGE - use INSERT ON DUPLICATE KEY UPDATE syntax explained.","og_url":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-14T16:14:09+00:00","article_modified_time":"2025-08-05T04:29:14+00:00","author":"Benjamin Taylor","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Benjamin Taylor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/"},"author":{"name":"Benjamin Taylor","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9"},"headline":"MySQL MERGE Alternative: INSERT ON DUPLICATE","datePublished":"2024-03-14T16:14:09+00:00","dateModified":"2025-08-05T04:29:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/"},"wordCount":214,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["INSERT ON DUPLICATE","MySQL MERGE","MySQL tutorial","SQL MERGE Alternative","UPSERT MySQL"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/","url":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/","name":"MySQL MERGE Alternative: INSERT ON DUPLICATE - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-14T16:14:09+00:00","dateModified":"2025-08-05T04:29:14+00:00","description":"Learn how to perform UPSERT operations in MySQL without MERGE - use INSERT ON DUPLICATE KEY UPDATE syntax explained.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-merge-statement-used-in-mysql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"MySQL MERGE Alternative: INSERT ON DUPLICATE"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9","name":"Benjamin Taylor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","caption":"Benjamin Taylor"},"url":"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=12629"}],"version-history":[{"count":3,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12629\/revisions"}],"predecessor-version":[{"id":156447,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12629\/revisions\/156447"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=12629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=12629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=12629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}