{"id":8436,"date":"2021-01-18T02:46:00","date_gmt":"2021-01-18T02:46:00","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=8436"},"modified":"2026-02-16T05:00:44","modified_gmt":"2026-02-16T05:00:44","slug":"how-to-avoid-inserting-duplicate-records-in-mysql","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/","title":{"rendered":"How To Avoid Inserting Duplicate Records in MySQL"},"content":{"rendered":"\n<p>MySQL is a popular database used by millions of websites and apps. It is used for storing and managing transactions. While doing so, often you may need to prevent duplicate insert while adding rows to MySQL table. If you have duplicate records in your table, then it can harm your data quality and reporting. In fact, if you have duplicate rows in your sales table, then it can lead to reporting inflated sales numbers. Therefore, it is essential to prevent inserting duplicate entries in your table. Once a duplicate entry has been created it is very difficult to find and <a href=\"https:\/\/ubiq.co\/tech-blog\/mysql-remove-duplicate-records\/\">remove duplicate rows<\/a>. You can easily avoid duplicate rows using INSERT IGNORE statement. In this article, we will look at how to avoid inserting duplicate records in MySQL.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#How_To_Avoid_Inserting_Duplicate_Records_in_MySQL\" >How To Avoid Inserting Duplicate Records in MySQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#1_Using_INSERT_IGNORE\" >1. Using INSERT IGNORE<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#Create_Unique_Index\" >Create Unique Index<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#Insert_data_using_INSERT_IGNORE\" >Insert data using INSERT IGNORE<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#What_is_INSERT_IGNORE\" >What is INSERT IGNORE?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#How_does_INSERT_IGNORE_work\" >How does INSERT IGNORE work?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#2_Using_On_Duplicate_Key\" >2. Using On Duplicate Key<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_To_Avoid_Inserting_Duplicate_Records_in_MySQL\"><\/span>How To Avoid Inserting Duplicate Records in MySQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are a couple of different ways to prevent inserting duplicate rows in MySQL. We will look at both these methods one by one. Here are the steps to avoid inserting duplicate records in MySQL. These are very useful especially when you insert multiple rows in table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_INSERT_IGNORE\"><\/span>1. Using INSERT IGNORE<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are two parts to keep in mind. First, you need to create a UNIQUE column index for your table. Next, you need to insert data using INSERT IGNORE to avoid duplicate records.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Unique_Index\"><\/span>Create Unique Index<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let us say you have the following table <em>sales(id, order_date, amount)<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; create table sales(<br>          id int, <br>          order_date date, <br>          amount int<br>          );<\/pre>\n\n\n\n<p>We add a UNIQUE index for <em>id<\/em> column using ALTER TABLE statement. This will ensure that all values of id column are unique. If you try to insert any row with duplicate id value, MySQL will immediately give an error and stop the insertion.<\/p>\n\n\n\n<p>Here is the syntax to create UNIQUE index in MySQL.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">alter table table_name ADD UNIQUE INDEX(\n        column_name1, \n        column_name2,\n        ...\n        column_nameN\n        );<\/pre>\n\n\n\n<p>Here is the SQL query to add UNIQUE index to <em>id<\/em> column in  <em>sales<\/em> table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; alter table sales ADD UNIQUE INDEX(id);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Insert_data_using_INSERT_IGNORE\"><\/span>Insert data using INSERT IGNORE<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Next, we will insert data using <a href=\"https:\/\/mariadb.com\/kb\/en\/insert-ignore\/\" target=\"_blank\" rel=\"noreferrer noopener\">INSERT IGNORE<\/a>.<\/p>\n\n\n\n<p>Before we proceed, let us look at what is INSERT IGNORE in MySQL and how does INSERT IGNORE work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_INSERT_IGNORE\"><\/span>What is INSERT IGNORE?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>INSERT IGNORE is a command that forces MySQL to ignore errors when you insert data into a MySQL table. So if you try to insert a row with duplicate primary key or duplicate values of unique columns, then MySQL will simply prevent the execution and give a warning instead of showing an error message. This is useful if you are, say, inserting many rows with some of them containing duplicate values. In this case, MySQL will simply drop those rows with duplicate primary key and insert rest of the rows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_does_INSERT_IGNORE_work\"><\/span>How does INSERT IGNORE work?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In our case, we have already created a UNIQUE index for <em>id<\/em> column. If we use INSERT statement to add data with duplicate rows, MySQL will throw an error and stop query execution after the first duplicate row. <\/p>\n\n\n\n<p>Instead, when we use INSERT IGNORE, MySQL will silently discard the insertion of  duplicate row without generating an error, and continue query execution. <\/p>\n\n\n\n<p>So if a record does not contain duplicate data, MySQL will insert it as usual. If it is a duplicate record, then MySQL will simply ignore it.<\/p>\n\n\n\n<p>Here is the SQL query to insert  data without duplicates in <em>sales<\/em> table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; insert ignore into sales(id,order_date,amount)<br>     values(1, '2021-01-01', 250),<br>     (1, '2021-01-01', 250),<br>     (2, '2021-01-02', 200),<br>     (3, '2021-01-03', 150),<br>     (2, '2021-01-02', 200);<br>Query OK, 3 rows affected (0.10 sec)<br><strong>Records: 5  Duplicates: 2  Warnings: 0<\/strong><br><br>mysql&gt; select * from sales;<br>+------+------------+--------+<br>| id   | order_date | amount |<br>+------+------------+--------+<br>|    1 | 2021-01-01 |    250 |<br>|    2 | 2021-01-02 |    200 |<br>|    3 | 2021-01-03 |    150 |<br>+------+------------+--------+<\/pre>\n\n\n\n<p>As you can see, MySQL avoids inserting duplicate rows, without generating any error. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_On_Duplicate_Key\"><\/span>2. Using On Duplicate Key<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this case, we simply add a &#8216;on duplicate key update&#8217; clause at the end of plain INSERT statement. This clause tells MySQL what to do in case it encounters a duplicate primary key value during insertion.<\/p>\n\n\n\n<p>Let us say you have the following table <em>sales(id, order_date, amount)<\/em> with id column as primary key.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; create table sales(<br>          id int primary key, <br>          order_date date, <br>          amount int<br>          );<\/pre>\n\n\n\n<p>Here is a simple example. We basically specify that when MySQL encounters a duplicate primary key value, it should simply update it by overwriting its column values, effectively dropping the duplicate row.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; insert into sales(id,order_date,amount)<br>     values(1, '2021-01-01', 250),<br>     (1, '2021-01-01', 250),<br>     (2, '2021-01-02', 200),<br>     (3, '2021-01-03', 150),<br>     (2, '2021-01-02', 200) <br>ON DUPLICATE KEY<br>    UPDATE order_date=order_date, amount=amount;<br><br>Query OK, 3 rows affected (0.01 sec)<br><strong>Records: 5  Duplicates: 0  Warnings: 0<\/strong><br><br>mysql&gt; select * from sales;<br>+----+------------+--------+<br>| id | order_date | amount |<br>+----+------------+--------+<br>|  1 | 2021-01-01 |    250 |<br>|  2 | 2021-01-02 |    200 |<br>|  3 | 2021-01-03 |    150 |<br>+----+------------+--------+<\/pre>\n\n\n\n<p>As you can see, in this case too, only 3 unique rows were inserted and the 2 duplicate rows were not inserted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt how to prevent inserting duplicate rows in table using INSERT IGNORE statement. It is much easier than finding duplicate rows in table and deleting them after insertion. This solution basically omits inserting rows with duplicate primary keys or columns with UNIQUE constraint. Instead of throwing an error, it will silently omit the record from insertion and proceed with the next insertion. This is really useful if you have a high transaction database with tons of insertions every moment.<\/p>\n\n\n\n<p>Need a reporting tool for MySQL? <a href=\"http:\/\/ubiq.co\/\">Ubiq<\/a>&nbsp;makes it easy to visualize data in minutes, and monitor in real-time dashboards.&nbsp;<a href=\"http:\/\/ubiq.co\/accounts\/register\">Try it<\/a>&nbsp;Today!<\/p>\n\n\n\n<p>Also read:<br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-convert-datetime-to-utc-in-mysql\/\">How to Convert Datetime to UTC in MySQL<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-convert-utc-to-local-time-in-mysql\/\">How to Convert UTC to Local time in MySQL<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-get-current-date-and-time-in-mysql\/\">How to Get Current Date &amp; Time in MySQL<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you may need to prevent duplicate insert while adding rows. Here is how to avoid inserting duplicate records in MySQL.<\/p>\n","protected":false},"author":1,"featured_media":8438,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324],"tags":[453,454],"class_list":["post-8436","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql","tag-avoid-duplicate","tag-prevent-duplicate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How To Avoid Inserting Duplicate Records in MySQL - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes you may need to prevent duplicate insert while adding rows. Here is how to avoid inserting duplicate records in MySQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Avoid Inserting Duplicate Records in MySQL - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes you may need to prevent duplicate insert while adding rows. Here is how to avoid inserting duplicate records in MySQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-18T02:46:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-16T05:00:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/Avoid-duplicate-records-in-mysql.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"600\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How To Avoid Inserting Duplicate Records in MySQL\",\"datePublished\":\"2021-01-18T02:46:00+00:00\",\"dateModified\":\"2026-02-16T05:00:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/\"},\"wordCount\":808,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/Avoid-duplicate-records-in-mysql.webp?fit=600%2C400&ssl=1\",\"keywords\":[\"avoid duplicate\",\"prevent duplicate\"],\"articleSection\":[\"MySQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/\",\"name\":\"How To Avoid Inserting Duplicate Records in MySQL - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/Avoid-duplicate-records-in-mysql.webp?fit=600%2C400&ssl=1\",\"datePublished\":\"2021-01-18T02:46:00+00:00\",\"dateModified\":\"2026-02-16T05:00:44+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes you may need to prevent duplicate insert while adding rows. Here is how to avoid inserting duplicate records in MySQL.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/Avoid-duplicate-records-in-mysql.webp?fit=600%2C400&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/Avoid-duplicate-records-in-mysql.webp?fit=600%2C400&ssl=1\",\"width\":600,\"height\":400,\"caption\":\"avoid duplicate records in mysql\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-avoid-inserting-duplicate-records-in-mysql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Avoid Inserting Duplicate Records in MySQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Avoid Inserting Duplicate Records in MySQL - Ubiq BI","description":"Sometimes you may need to prevent duplicate insert while adding rows. Here is how to avoid inserting duplicate records in MySQL.","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:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/","og_locale":"en_US","og_type":"article","og_title":"How To Avoid Inserting Duplicate Records in MySQL - Ubiq BI","og_description":"Sometimes you may need to prevent duplicate insert while adding rows. Here is how to avoid inserting duplicate records in MySQL.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2021-01-18T02:46:00+00:00","article_modified_time":"2026-02-16T05:00:44+00:00","og_image":[{"width":600,"height":400,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/Avoid-duplicate-records-in-mysql.webp","type":"image\/webp"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How To Avoid Inserting Duplicate Records in MySQL","datePublished":"2021-01-18T02:46:00+00:00","dateModified":"2026-02-16T05:00:44+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/"},"wordCount":808,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/Avoid-duplicate-records-in-mysql.webp?fit=600%2C400&ssl=1","keywords":["avoid duplicate","prevent duplicate"],"articleSection":["MySQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/","name":"How To Avoid Inserting Duplicate Records in MySQL - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/Avoid-duplicate-records-in-mysql.webp?fit=600%2C400&ssl=1","datePublished":"2021-01-18T02:46:00+00:00","dateModified":"2026-02-16T05:00:44+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes you may need to prevent duplicate insert while adding rows. Here is how to avoid inserting duplicate records in MySQL.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/Avoid-duplicate-records-in-mysql.webp?fit=600%2C400&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/Avoid-duplicate-records-in-mysql.webp?fit=600%2C400&ssl=1","width":600,"height":400,"caption":"avoid duplicate records in mysql"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How To Avoid Inserting Duplicate Records in MySQL"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/Avoid-duplicate-records-in-mysql.webp?fit=600%2C400&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-2c4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=8436"}],"version-history":[{"count":4,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8436\/revisions"}],"predecessor-version":[{"id":10617,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8436\/revisions\/10617"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/8438"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=8436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=8436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=8436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}