This morning I woke up with this question on my
mind. If I reseed an Identity column in
a table with existing rows will the new rows account for the existing identity
numbers and skip them as it increases the numbers. The answer is no and here is how I proved
it.
First, I created a table that I could test in
CREATE TABLE [dbo].[T_SWL_3](
[SWL_id] [int] Identity (1,1) not NULL,[SWL_Text] [varchar] (50) NULL,
[SWL_Date] [datetime] NULL DEFAULT (getdate()),
CONSTRAINT [PK_T_SWL_3] PRIMARY KEY CLUSTERED
(
[SWL_id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
This table has an Identity column seeded starting at 1 with an increment of 1. It also has a date defaulting to the getdate() when the row is inserted.
Second I placed some rows in the table and deleted a
few to create holes in the identity column.
INSERT INTO [dbo].[T_SWL_3]
([SWL_Text])Select 'Test 1'
Union
Select 'Test 2'
Union
Select 'Test 3'
Union
Select 'Test 4'
Union
Select 'Test 5'
Union
Select 'Test 6'
Go
DELETE FROM [dbo].[T_SWL_3]
WHERE [SWL_id] in (2,4,5)GO
Third, I added some more rows to make sure that
Identity would continue and it does.
INSERT INTO [dbo].[T_SWL_3]
([SWL_Text])Select 'Test 1a'
Union
Select 'Test 2a'
Go
SELECT [SWL_id]
,[SWL_Text],[SWL_Date]
FROM [dbo].[T_SWL_3]
GO
At this point, here is what the table looks like.
|
SWL_id
|
SWL_Text
|
SWL_Date
|
|
1
|
Test 1
|
10/31/13 6:53 AM
|
|
3
|
Test 3
|
10/31/13 6:53 AM
|
|
6
|
Test 6
|
10/31/13 6:53 AM
|
|
7
|
Test 1a
|
10/31/13 6:53 AM
|
|
8
|
Test 2a
|
10/31/13 6:53 AM
|
Fourth, I reseeded the table with this command
starting back at 1.
DBCC CHECKIDENT ('T_SWL_3', RESEED, 1);
Fifth, I inserted new rows to see what would
happen. It fails and I get this error
message.
Select 'Test 1c' Item
Union Select 'Test 2c' Item
Go
SELECT [SWL_id]
,[SWL_Text]
,[SWL_Date]
FROM [dbo].[T_SWL_3]
GO
Msg 2627, Level 14, State 1, Line 5
Violation of PRIMARY KEY constraint 'PK_T_SWL_3'.
Cannot insert duplicate key in object 'dbo.T_SWL_3'. The duplicate key value is
(3).
The statement has been terminated.
The primary key constraint must be unique and when
the identity process within SQL Server tries to use 3 it is already there and the insert fails.
Moral of the story, either remove all existing rows
before the reseed, renumber the Identities or don’t reseed. Unfortunately, the identity is generally
used as the link between two tables so changing the numbers on data that needs
to be retained is probably out of the question because all connecting tables
would need to change as well. One
possibility is to start the seed at a very low number and this allows it more
room to grow.
Columns that are int could have their seeds starting
at -2,147,483,648 and bigints at
--9,223,372,036,854,775,808. If the id
is used to just link tables and no one will see it, there is no reason to do
this and it doubles your row capacity for the identity column.
Here is what my table looks like if I do
that.
CREATE TABLE [dbo].[T_SWL_3](
[SWL_id] [int] Identity (-2147483648,1) not NULL,[SWL_Text] [varchar] (50) NULL,
[SWL_Date] [datetime] NULL DEFAULT (getdate()),
CONSTRAINT [PK_T_SWL_3] PRIMARY KEY CLUSTERED
(
[SWL_id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
([SWL_Text])
Select 'Test 1'
Union
Select 'Test 2'
Union
Select 'Test 3'
Union
Select 'Test 4'
Union
Select 'Test 5'
Union
Select 'Test 6'
Go
SELECT [SWL_id]
,[SWL_Text],[SWL_Date]
FROM [dbo].[T_SWL_3]
GO
|
SWL_id
|
SWL_Text
|
SWL_Date
|
|
-2147483648
|
Test 1
|
10/31/13 7:03 AM
|
|
-2147483647
|
Test 2
|
10/31/13 7:03 AM
|
|
-2147483646
|
Test 3
|
10/31/13 7:03 AM
|
|
-2147483645
|
Test 4
|
10/31/13 7:03 AM
|
|
-2147483644
|
Test 5
|
10/31/13 7:03 AM
|
|
-2147483643
|
Test 6
|
10/31/13 7:03 AM
|