patternsqlModerate
Are auto incrementing identities always given in the order of inserts in SQL Server?
Viewed 0 times
theorderareautoalwaysinsertssqlincrementingserveridentities
Problem
If I create a simple table like this:
And then I insert into the table 100 times, within the same transaction, like this...
Is there any guarantee that the inserted rows will be given identity values in the order in which the insert statements are executed?
More specifically, if I run this query after the inserts...
Will I always see this...
Or, does SQL not guarantee this?
CREATE TABLE SimpleTable (
[Id] INT IDENTITY(1,1) NOT NULL,
[Value] VARCHAR(10) NOT NULL,
CONSTRAINT [PK_Email] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]And then I insert into the table 100 times, within the same transaction, like this...
TRUNCATE TABLE SimpleTable -- ensure reset identity seed
BEGIN TRAN
INSERT INTO SimpleTable(Value) VALUES('First')
INSERT INTO SimpleTable(Value) VALUES('Second')
INSERT INTO SimpleTable(Value) VALUES('Third')
INSERT INTO SimpleTable(Value) VALUES('Fourth')
/* ... etc */
INSERT INTO SimpleTable(Value) VALUES('Hundredth')
COMMIT TRANIs there any guarantee that the inserted rows will be given identity values in the order in which the insert statements are executed?
More specifically, if I run this query after the inserts...
SELECT * FROM SimpleTable ORDER BY 1Will I always see this...
Id Value
----------------------------------------------
1 First
2 Second
3 Third
4 Fourth
... etc
100 HundredthOr, does SQL not guarantee this?
Solution
If this is the only transaction on that table, then yes. However, another transaction could also do an insert at the same time, and be somewhere in between 1-100, unless you use
From IDENTITY (Property) (Transact-SQL) in the product documentation:
The identity property on a column does not guarantee the following:
-
Uniqueness of the value – Uniqueness must be enforced by using a PRIMARY KEY or UNIQUE constraint or UNIQUE index.
-
Consecutive values within a transaction – A transaction inserting multiple rows is not guaranteed to get consecutive values for the rows because other concurrent inserts might occur on the table. If values must be consecutive then the transaction should use an exclusive lock on the table or use the SERIALIZABLE isolation level.
-
Consecutive values after server restart or other failures –SQL Server might cache identity values for performance reasons and some of the assigned values can be lost during a database failure or server restart. This can result in gaps in the identity value upon insert. If gaps are not acceptable then the application should use its own mechanism to generate key values. Using a sequence generator with the NOCACHE option can limit the gaps to transactions that are never committed.
-
Reuse of values – For a given identity property with specific seed/increment, the identity values are not reused by the engine. If a particular insert statement fails or if the insert statement is rolled back then the consumed identity values are lost and will not be generated again. This can result in gaps when the subsequent identity values are generated
SERIALIZABLE isolation.From IDENTITY (Property) (Transact-SQL) in the product documentation:
The identity property on a column does not guarantee the following:
-
Uniqueness of the value – Uniqueness must be enforced by using a PRIMARY KEY or UNIQUE constraint or UNIQUE index.
-
Consecutive values within a transaction – A transaction inserting multiple rows is not guaranteed to get consecutive values for the rows because other concurrent inserts might occur on the table. If values must be consecutive then the transaction should use an exclusive lock on the table or use the SERIALIZABLE isolation level.
-
Consecutive values after server restart or other failures –SQL Server might cache identity values for performance reasons and some of the assigned values can be lost during a database failure or server restart. This can result in gaps in the identity value upon insert. If gaps are not acceptable then the application should use its own mechanism to generate key values. Using a sequence generator with the NOCACHE option can limit the gaps to transactions that are never committed.
-
Reuse of values – For a given identity property with specific seed/increment, the identity values are not reused by the engine. If a particular insert statement fails or if the insert statement is rolled back then the consumed identity values are lost and will not be generated again. This can result in gaps when the subsequent identity values are generated
Context
StackExchange Database Administrators Q#138152, answer score: 11
Revisions (0)
No revisions yet.