﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Ankur  / Duplicate value in Identity column / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 18 Jun 2013 18:01:18 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Good tricky question and yes, duplicates are possible to occur though depending on their intended purposes, it may usually not be recommended and therefore different sequential numbers ranges are used in such a cases that duplicates are to be avoided.Thanks</description><pubDate>Wed, 07 Mar 2012 14:19:01 GMT</pubDate><dc:creator>zymos</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>I would add that you can also insert duplicate into identity by reseeding IDENTITY.dbcc checkident(table_name,reseed,0)and it will start from beginning again, unless as mentioned above you have unique key on the column.</description><pubDate>Tue, 05 Oct 2010 11:40:07 GMT</pubDate><dc:creator>BowieRules!</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>I thought identity_insert would allow duplicates but google said no.... You can't always trust information on internet :-D</description><pubDate>Sun, 26 Sep 2010 22:57:04 GMT</pubDate><dc:creator>Dhruvesh Shah</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Yow... Good question.</description><pubDate>Tue, 21 Sep 2010 10:48:20 GMT</pubDate><dc:creator>skrilla99</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>[quote]No, that would not be a good idea. The IDENTITY property does not guarantee that there will not be any gaps. And in practice, there [b]will[/b] be gaps. If the business requires consecutive numbering, IDENTITY is not a good option.[/quote]I agree if gaps are not acceptable, but if the possibility of gaps in the minor number is not a problem for the business rules, then I still believe that this is a reasonable solution.</description><pubDate>Thu, 19 Aug 2010 06:40:10 GMT</pubDate><dc:creator>andrewd.smith</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>[quote][b]andrewd.smith (8/18/2010)[/b][hr]I can imagine some reasonable uses for an IDENTITY column where duplicates are allowed / expected.Say we have some sort of versioning system where the version number comprises a major number and a minor number. The business rules state that the minor version number is reset whenever the major version number changes. The decision to change major version numbers is triggered by an explicit user action and is infrequent, but the minor number should increment automatically whenever any change is logged (by inserting a new table row) which is expected to be a frequent occurrence. The minor number could be modelled reasonably by an IDENTITY column that is reset to 0 whenever the major number is incremented. The table that implements this versioning system could have a structure something like the following:[code="sql"]CREATE TABLE dbo.Version (   MajorNumber int NOT NULL,   MinorNumber int NOT NULL IDENTITY(0, 1),   DateStamp datetime NOT NULL DEFAULT(GETDATE()),   Comment nvarchar(1000) NULL,   CONSTRAINT PK_Version PRIMARY KEY CLUSTERED (MajorNumber, MinorNumber),   CONSTRAINT CK_VersionNumber CHECK (MajorNumber &amp;gt;= 1 AND MinorNumber &amp;gt;= 0))[/code][/quote]No, that would not be a good idea. The IDENTITY property does not guarantee that there will not be any gaps. And in practice, there [b]will[/b] be gaps. If the business requires consecutive numbering, IDENTITY is not a good option.There are some good reasons for manually inserting values in an IDENTITY column, as already posted to this discussion. But I see no good reasons to allow duplicate values in the IDENTITY column. And that is exactly what makes this QotD so valuable - as a reminder to always add a PRIMARY KEY or UNIQUE constraint when we add the IDENTITY property to a column, because the IDENTITY property alone does not guarantee uniqueness.</description><pubDate>Thu, 19 Aug 2010 00:55:51 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>I can imagine some reasonable uses for an IDENTITY column where duplicates are allowed / expected.Say we have some sort of versioning system where the version number comprises a major number and a minor number. The business rules state that the minor version number is reset whenever the major version number changes. The decision to change major version numbers is triggered by an explicit user action and is infrequent, but the minor number should increment automatically whenever any change is logged (by inserting a new table row) which is expected to be a frequent occurrence. The minor number could be modelled reasonably by an IDENTITY column that is reset to 0 whenever the major number is incremented. The table that implements this versioning system could have a structure something like the following:[code="sql"]CREATE TABLE dbo.Version (   MajorNumber int NOT NULL,   MinorNumber int NOT NULL IDENTITY(0, 1),   DateStamp datetime NOT NULL DEFAULT(GETDATE()),   Comment nvarchar(1000) NULL,   CONSTRAINT PK_Version PRIMARY KEY CLUSTERED (MajorNumber, MinorNumber),   CONSTRAINT CK_VersionNumber CHECK (MajorNumber &amp;gt;= 1 AND MinorNumber &amp;gt;= 0))[/code]</description><pubDate>Wed, 18 Aug 2010 16:46:38 GMT</pubDate><dc:creator>andrewd.smith</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>[quote][b]SwayneBell (8/18/2010)[/b][hr]Hi Daniel;That seems a perfectly sensible use for identity insert. However, in your example, I don't see it as inserting a duplicate value, more like reusing an identity value that was previously used. My question relates to why would you want to have two people (for example) with the same Identity value as their identifier?[/quote]You're right, and I can't think of any good reasons for inserting duplicate values into an identity column either. But I don't object to having the freedom to do so. It's useful to know that if uniqueness is required it needs to be enforced separately though. It's something I'd not considered before.</description><pubDate>Wed, 18 Aug 2010 07:18:25 GMT</pubDate><dc:creator>daniel.noble</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Hi Daniel;That seems a perfectly sensible use for identity insert. However, in your example, I don't see it as inserting a duplicate value, more like reusing an identity value that was previously used. My question relates to why would you want to have two people (for example) with the same Identity value as their identifier?</description><pubDate>Wed, 18 Aug 2010 07:00:57 GMT</pubDate><dc:creator>SteveBell</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>[quote][b]dbowlin (8/16/2010)[/b][hr]Identity insert can be very dangerous. For a long time I couldn't see a use for it.  Then I needed it to get 2 tables in 2 different databases into sync after they had fallen out of sync for some unknown reason.  It saved me a lot of effort.[/quote]I move data from a Live table (with an identity on the PKey) into an Archive table. Just occasionally I need to move a row back into the Live table. Identity insert allows me to move it back with its original ID, thus maintaining references to this row from other tables. So there's one use for it.</description><pubDate>Wed, 18 Aug 2010 03:32:14 GMT</pubDate><dc:creator>daniel.noble</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>In one scenario we had to copy some data from live DB to test DB. Since the table was having an identity column we were not able to make the data consistent hence we used the above mentioned feature...:w00t:</description><pubDate>Tue, 17 Aug 2010 05:09:04 GMT</pubDate><dc:creator>AppSup_dba</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Yep,I got this one wrong also.However, it begs the question, "why would you want an identity column in the first place if you intend to insert a duplicate value?"</description><pubDate>Tue, 17 Aug 2010 04:57:08 GMT</pubDate><dc:creator>SteveBell</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Nice question.</description><pubDate>Tue, 17 Aug 2010 00:34:47 GMT</pubDate><dc:creator>VM-723206</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>[quote][b]OCTom (8/16/2010)[/b][hr]Perhaps Microsoft should change it so that a PK or unique constraint must accompany an identity column? Like you say, it doesn't make sense to have it this way and is a potential problem.[/quote]I don't see that happening. It could potentially break existing code that relies on identity values not being unique (why? don't ask me - people do the craziest things in SQL Server). And if MS would ban every option that you can use to shoot yourself in the foot, there wouldn't be a lot left...</description><pubDate>Mon, 16 Aug 2010 14:34:35 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>[quote][b]SanjayAttray (8/16/2010)[/b][hr]Yes you can insert but should be avoided by having a unique constraint or Pkey on identity column.  Since, it doesn't make much sense, if you want to put duplicate value in identity column then why add identity option to column int.  Just leave it as is.[/quote]Perhaps Microsoft should change it so that a PK or unique constraint must accompany an identity column? Like you say, it doesn't make sense to have it this way and is a potential problem. </description><pubDate>Mon, 16 Aug 2010 14:08:52 GMT</pubDate><dc:creator>OCTom</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Nice question.</description><pubDate>Mon, 16 Aug 2010 08:50:46 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Interesting.  Very surprised to learn you can enter duplicate values when inserting explicit values.  Further surprised to learn that you can get duplicate values by resetting the identity seed, according to this person:http://beyondrelational.com/blogs/jacob/archive/2009/02/03/sql-server-identity-why-do-i-have-duplicate-identity-values.aspx</description><pubDate>Mon, 16 Aug 2010 08:39:47 GMT</pubDate><dc:creator>Rose Bud</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>[quote][b]SanjayAttray (8/16/2010)[/b][hr]Since, it doesn't make much sense, if you want to put duplicate value in identity column then why add identity option to column int.  Just leave it as is.[/quote]I think the idea behind this question is to warn people that just using IDENTITY is not enough. Not everybody knows that duplicate entries can still happen with an IDENTITY, so that is a valuable lesson learned with this question.</description><pubDate>Mon, 16 Aug 2010 08:39:35 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Yes you can insert but should be avoided by having a unique constraint or Pkey on identity column.  Since, it doesn't make much sense, if you want to put duplicate value in identity column then why add identity option to column int.  Just leave it as is.</description><pubDate>Mon, 16 Aug 2010 07:56:38 GMT</pubDate><dc:creator>SanjayAttray</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Only one person can insert identity on a column at a time, so [b][i][u]WE[/u][/i][/b] can not... :hehe:</description><pubDate>Mon, 16 Aug 2010 07:09:03 GMT</pubDate><dc:creator>SanDroid</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Identity insert can be very dangerous. For a long time I couldn't see a use for it.  Then I needed it to get 2 tables in 2 different databases into sync after they had fallen out of sync for some unknown reason.  It saved me a lot of effort.</description><pubDate>Mon, 16 Aug 2010 06:43:33 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>I have to admint I wouldn't have gotten this one right, except for a similar question a few months ago.  I still don't understand why one would want to create an identity and then break the rules of the identity.  Seems like trouble   :-)</description><pubDate>Mon, 16 Aug 2010 06:26:32 GMT</pubDate><dc:creator>pjdiller</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Good Question...</description><pubDate>Mon, 16 Aug 2010 04:57:43 GMT</pubDate><dc:creator>Danny Ocean</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>And i thought i only didnt knew it :-)</description><pubDate>Mon, 16 Aug 2010 01:41:26 GMT</pubDate><dc:creator>AppSup_dba</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>[quote][b]ziangij (8/15/2010)[/b][hr]good one... duplicates can only be avoided by having a constraint in place...[/quote]...or by having a unique index in place.</description><pubDate>Mon, 16 Aug 2010 01:25:14 GMT</pubDate><dc:creator>Christian Buettner-167247</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Nice question. Usually an Identity column is used as a primary key (and thus having a unique clustered index), which will prevent the insertion of duplicate values.</description><pubDate>Mon, 16 Aug 2010 00:26:33 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>good one... duplicates can only be avoided by having a constraint in place...</description><pubDate>Sun, 15 Aug 2010 23:06:17 GMT</pubDate><dc:creator>ziangij</dc:creator></item><item><title>RE: Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>hi,I know that we can add explicit values to an identity column using Identity_Insert on. but i thought that identify column will not allow duplicate columns.good question.lost one point but learned one new point.</description><pubDate>Sun, 15 Aug 2010 22:17:05 GMT</pubDate><dc:creator>malleswarareddy_m</dc:creator></item><item><title>Duplicate value in Identity column</title><link>http://www.sqlservercentral.com/Forums/Topic969419-2762-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/70599/"&gt;Duplicate value in Identity column&lt;/A&gt;[/B]</description><pubDate>Sat, 14 Aug 2010 12:46:54 GMT</pubDate><dc:creator>AppSup_dba</dc:creator></item></channel></rss>