{"id":41442,"date":"2021-05-03T21:47:29","date_gmt":"2021-05-03T21:47:29","guid":{"rendered":"https:\/\/www.oracletutoring.ca\/blog\/?p=41442"},"modified":"2021-05-03T21:53:57","modified_gmt":"2021-05-03T21:53:57","slug":"comp-sci-sql-server-tables-cant-insert-an-integer-id-primary-key","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/comp-sci-sql-server-tables-cant-insert-an-integer-id-primary-key\/","title":{"rendered":"Comp Sci: SQL Server tables: can&#8217;t insert an integer ID (primary key)?"},"content":{"rendered":"\n<h2>Self-tutoring about computer science: the tutor mentions a bump he hit and the investigation he performed because of it.<\/h2>\n<p>\nToday I was using SQL Server and decided to experiment: I changed an outside program to set the integer id (which is also the primary key) during an insert query. Before, I&#8217;d allowed the database to set each new row&#8217;s id &#8211; which is typical. I made the Insert query from Linq, using Visual Basic. My other queries from that context have all worked. In fact, I&#8217;m impressed with Linq, but I guess that&#8217;s another story for another post.<\/p>\n<p>I wasn&#8217;t surprised when the insert that included the new id didn&#8217;t work; databases have rules. From one to another, they may not always be the same, and there&#8217;s many of them, in total. However, most of the rules, I&#8217;ll admit, you won&#8217;t even notice if you just follow common sense. Setting the id from outside the database isn&#8217;t necessarily advised, unless you&#8217;re aware what you&#8217;re doing: it&#8217;s safer for the database to just not let you try.<\/p>\n<p>\nThe typical, charming way that a database decides if you know what you&#8217;re doing, and should therefore be allowed to circumvent a rule, is very simple. If you really know the database, you&#8217;ll know the rule is the default, and how to unset it. That knowledge, however, is usually much less common than &#8220;create, read, update or delete&#8221; &#8211; only someone savvy enough to understand the rule and why it&#8217;s there is likely to know how to unset it.<\/p>\n<p>I didn&#8217;t change the default rule; I just changed the database table back to auto-setting each new id, then omitted the id from the Insert query. Since, it works as expected.<\/p>\n<p>For any dot net programmer doing Linq queries who arrives at this post, I&#8217;ll share with you what I found:<\/p>\n<ol>\n<li>It seems that the boolean that prevents the user from setting the primary key integer id is IDENTITY_INSERT. Its default is FALSE aka OFF. I&#8217;ve only read about it.<\/li>\n<li>The search phrase &#8220;microsoft docs identity insert&#8221; gives me the answers I would need.<\/li>\n<\/ol>\n<p>Source:<\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/1334012\/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity\">stackoverflow.com<\/a><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/statements\/set-identity-insert-transact-sql?view=sql-server-ver15\">docs.microsoft.com<\/a><\/p>\nJack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.\n","protected":false},"excerpt":{"rendered":"<p>Self-tutoring about computer science: the tutor mentions a bump he hit and the investigation he performed because of it. Today I was using SQL Server and decided to experiment: I changed an outside program to set the integer id (which &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/comp-sci-sql-server-tables-cant-insert-an-integer-id-primary-key\/\"> <span class=\"screen-reader-text\">Comp Sci: SQL Server tables: can&#8217;t insert an integer ID (primary key)?<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-41442","post","type-post","status-publish","format-standard","hentry","category-computer-science"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/41442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=41442"}],"version-history":[{"count":12,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/41442\/revisions"}],"predecessor-version":[{"id":41454,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/41442\/revisions\/41454"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=41442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=41442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=41442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}