{"id":6356,"date":"2022-02-21T06:12:00","date_gmt":"2022-02-21T05:12:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=6356"},"modified":"2023-03-27T17:06:37","modified_gmt":"2023-03-27T15:06:37","slug":"violation-of-unique-key-constraint","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/","title":{"rendered":"Violation of unique key constraint cannot insert duplicate SQL Server error"},"content":{"rendered":"\n<h4 class=\"has-text-align-center wp-block-heading\"><strong><em>2 solutions to fix the SQL Server error : violation of unique key constraint cannot insert duplicate key.<\/em><\/strong><\/h4>\n\n\n\n<p>How to avoid and fix the Violation of unique key constraint, cannot insert duplicate key error during SQL Server development? Insert or update data in an SQL Server table with a simple query? Here are two simple solutions to execute an update or insert and avoid errors. The SQL Server error message is : Violation of UNIQUE KEY constraint . Cannot insert duplicate key in object . The duplicate key value is&#8230; The error appears because the line you are inserting already exists in the target table.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center 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\"><\/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:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#1-create-the-sample-sql-server-table-with-a-constraint\" >1. Create the sample SQL Server table with a constraint<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#2-avoid-the-violation-of-unique-key-constraint-error-using-insert-or-update\" >2. Avoid the violation of unique key constraint error using insert or update<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#3-fix-the-insertion-of-duplicate-key-error-with-an-update-or-insert\" >3. Fix the insertion of duplicate key error with an update or insert<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#4-conclusion-on-t-sql-unique-key-constraint-error\" >4. Conclusion on T-SQL unique key constraint error<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1-create-the-sample-sql-server-table-with-a-constraint\"><\/span>1. Create the sample SQL Server table with a constraint<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Indeed, this SQL Server script creates a table with two <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/data-types\/data-types-transact-sql?view=sql-server-ver15\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">columns<\/a>: [Month] and [Amount]. In other words the month and the number of sales realized. Please note that the [Month] column as the UNIQUE keyword, so the table cannot store two lines for the same month. Please execute this query first before using the solution queries. It&#8217;s creating a Sales sample table through these steps and results: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Drop the table if it already in the database<\/li>\n\n\n\n<li>Create the sales sample table with a unicity constraint on the Month column <\/li>\n\n\n\n<li>Insert 2 lines with the same month, i.e. <em>January<\/em><\/li>\n\n\n\n<li>Once the script is executed, it trigger the unique constraint error.<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- If the SALES table already exists in the database, we delete it\nIF EXISTS(\nSELECT 1 FROM sys.objects\nWHERE object_id = object_id(N'[dbo].[SALES]') AND type in (N'U')\n)\nBEGIN\nDROP TABLE [dbo].[SALES]\nEND\nGO\n\n-- Create the SALES table with the MONTH column declared as unique\nCREATE TABLE [dbo].[SALES] (\n[MONTH] nvarchar(20) UNIQUE,\n[AMOUNT] numeric(5)\n)\nGO\n\n-- Insert the sampla data\nINSERT INTO dbo.SALES ( MONTH, AMOUNT ) VALUES ( N'January', 1000);\nINSERT INTO dbo.SALES ( MONTH, AMOUNT ) VALUES ( N'January', 2000);\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"610\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/t-sql-violation-of-unique-key-constraint-error.jpg\" alt=\"SQL Server error : Violation of UNIQUE KEY constraint cannot insert duplicate key in SSMS\" class=\"wp-image-24012\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/t-sql-violation-of-unique-key-constraint-error.jpg 820w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/t-sql-violation-of-unique-key-constraint-error-300x223.jpg 300w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/t-sql-violation-of-unique-key-constraint-error-768x571.jpg 768w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/t-sql-violation-of-unique-key-constraint-error-80x60.jpg 80w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><figcaption class=\"wp-element-caption\"><strong><em>SQL Server error : Violation of UNIQUE KEY constraint cannot insert duplicate key in SSMS<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p><strong>The error message displayed:<\/strong><\/p>\n\n\n\n<p>(1 row affected)<br><em>Msg 2627, Level 14, State 1, Line 20<br>Violation of UNIQUE KEY constraint &#8216;UQ__SALES__03EA0046C8F30675&#8217;. Cannot insert duplicate key in object &#8216;dbo.SALES&#8217;. The duplicate key value is (January).<br>The statement has been terminated.<\/em><\/p>\n\n\n\n<p>To avoid the SQL Server violation of unique key error, 2 different but very similar solutions exists: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Do a simple INSERT or UPDATE : check if the line is in the table, and do an INSERT or an UPDATE.<\/li>\n\n\n\n<li>Do an UPDATE on the table and count the lines updated, if it is 0 then perform the INSERT.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-avoid-the-violation-of-unique-key-constraint-error-using-insert-or-update\"><span class=\"ez-toc-section\" id=\"2-avoid-the-violation-of-unique-key-constraint-error-using-insert-or-update\"><\/span>2. Avoid the violation of unique key constraint error using insert or update<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Firstly, one solution is to use the EXISTS() function to check if a line with &#8216;January&#8217; Month value already exists in the table. If no line exists then we insert the sale for 2000$ instead of 1000$.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IF NOT EXISTS(SELECT * FROM dbo.SALES WHERE MONTH = 'January')\nBEGIN\n    INSERT INTO dbo.SALES ( MONTH, AMOUNT )\n    VALUES ( N'January', 2000)\nEND\nELSE\nBEGIN\n    UPDATE dbo.SALES\n    SET AMOUNT = 2000\n    WHERE MONTH = 'January'\nEND\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-\">&nbsp;<\/h3>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-3-fix-the-insertion-of-duplicate-key-error-with-an-update-or-insert\"><span class=\"ez-toc-section\" id=\"3-fix-the-insertion-of-duplicate-key-error-with-an-update-or-insert\"><\/span>3. Fix the insertion of duplicate key error with an update or insert<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Secondly, the other solution is to update the table, then check the number of lines updated. If the number equals zero, then no line exists in the table. And we can insert our line with 2000$ as sales amount using a classical INSERT statement.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">UPDATE dbo.SALES\nSET AMOUNT = 2000\nWHERE MONTH = 'January'\n\nIF @@ROWCOUNT = 0\nBEGIN\n INSERT INTO dbo.SALES ( MONTH, AMOUNT )\n VALUES ( N'January', 2000)\nEND\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-conclusion-on-t-sql-unique-key-constraint-error\"><\/span>4. Conclusion on T-SQL unique key constraint error<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In conclusion, we have explored two distinct solutions to avoid the <strong><em>Violation of unique key constraint cannot insert duplicate key<\/em><\/strong> error in SQL Server. Both methods utilize INSERT and UPDATE statements to handle this issue effectively. The first approach checks if the record exists and performs either an INSERT or an UPDATE, while the second method uses an UPDATE followed by an INSERT if no rows were affected by the UPDATE.<\/p>\n\n\n\n<p>By employing these strategies, you can prevent the violation of unique key constraints and ensure data integrity within your database. Additionally, understanding these techniques will help you to address other related errors, such as the <a href=\"https:\/\/expert-only.com\/en\/errors\/arithmetic-overflow-error\/\">Arithmetic overflow error,<\/a> and improve your overall SQL Server expertise.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"z4M1Z6lqjJ\"><a href=\"https:\/\/expert-only.com\/en\/errors\/arithmetic-overflow-error\/\">Arithmetic overflow error converting expression to data type int<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Arithmetic overflow error converting expression to data type int&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/errors\/arithmetic-overflow-error\/embed\/#?secret=OOCvWe8TYX#?secret=z4M1Z6lqjJ\" data-secret=\"z4M1Z6lqjJ\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>2 solutions to fix the SQL Server error : violation of unique key constraint cannot insert duplicate key. How to avoid and fix the Violation of unique key constraint, cannot insert duplicate key error during SQL Server development? Insert <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/\" title=\"Violation of unique key constraint cannot insert duplicate SQL Server error\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6082,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[486],"tags":[],"class_list":{"0":"post-6356","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-errors"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Violation of unique key constraint cannot insert duplicate - T-SQL<\/title>\n<meta name=\"description\" content=\"The solution to avoid the SQL Server error Violation of unique key constraint cannot insert a duplicate key is to use the INSERT OR UPDATE.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Violation of unique key constraint cannot insert duplicate SQL Server error\" \/>\n<meta property=\"og:description\" content=\"The solution to avoid the SQL Server error Violation of unique key constraint cannot insert a duplicate key is to use the INSERT OR UPDATE.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-21T05:12:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-27T15:06:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\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:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"Violation of unique key constraint cannot insert duplicate SQL Server error\",\"datePublished\":\"2022-02-21T05:12:00+00:00\",\"dateModified\":\"2023-03-27T15:06:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/\"},\"wordCount\":570,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg\",\"articleSection\":[\"SQL Server errors\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/\",\"url\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/\",\"name\":\"Violation of unique key constraint cannot insert duplicate - T-SQL\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg\",\"datePublished\":\"2022-02-21T05:12:00+00:00\",\"dateModified\":\"2023-03-27T15:06:37+00:00\",\"description\":\"The solution to avoid the SQL Server error Violation of unique key constraint cannot insert a duplicate key is to use the INSERT OR UPDATE.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Violation of unique key constraint cannot insert duplicate SQL Server error\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Violation of unique key constraint cannot insert duplicate - T-SQL","description":"The solution to avoid the SQL Server error Violation of unique key constraint cannot insert a duplicate key is to use the INSERT OR UPDATE.","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:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/","og_locale":"en_US","og_type":"article","og_title":"Violation of unique key constraint cannot insert duplicate SQL Server error","og_description":"The solution to avoid the SQL Server error Violation of unique key constraint cannot insert a duplicate key is to use the INSERT OR UPDATE.","og_url":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2022-02-21T05:12:00+00:00","article_modified_time":"2023-03-27T15:06:37+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"Violation of unique key constraint cannot insert duplicate SQL Server error","datePublished":"2022-02-21T05:12:00+00:00","dateModified":"2023-03-27T15:06:37+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/"},"wordCount":570,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg","articleSection":["SQL Server errors"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/","url":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/","name":"Violation of unique key constraint cannot insert duplicate - T-SQL","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg","datePublished":"2022-02-21T05:12:00+00:00","dateModified":"2023-03-27T15:06:37+00:00","description":"The solution to avoid the SQL Server error Violation of unique key constraint cannot insert a duplicate key is to use the INSERT OR UPDATE.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/08\/board-3317498_1920.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/errors\/violation-of-unique-key-constraint\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"Violation of unique key constraint cannot insert duplicate SQL Server error"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/6356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=6356"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/6356\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/6082"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=6356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=6356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=6356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}