Questions tagged [identity]

Creates an identity column in a table. This property is used with the CREATE TABLE and ALTER TABLE Transact-SQL statements.

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.

REFERENCES

IDENTITY (Property) (Transact-SQL)

198 questions
74
votes
10 answers

Why do people recommend not using the name "Id" for an identity column?

I was taught not to use the name Id for the identity column of my tables, but lately I've just been using it anyways because it's simple, short, and very descriptive about what the data actually is. I've seen people suggest prefixing Id with the…
Rachel
  • 8,547
  • 20
  • 51
  • 74
51
votes
2 answers

Best way to get last identity inserted in a table

Which one is the best option to get the identity value I just generated via an insert? What is the impact of these statements in terms of performance? SCOPE_IDENTITY() Aggregate function MAX() SELECT TOP 1 IdentityColumn FROM TableName ORDER BY…
AA.SC
  • 4,073
  • 4
  • 28
  • 45
48
votes
10 answers

How do I copy a table with SELECT INTO but ignore the IDENTITY property?

I have a table with identity column say: create table with_id ( id int identity(1,1), val varchar(30) ); It's well known, that this select * into copy_from_with_id_1 from with_id; results in copy_from_with_id_1 with identity on id too. The…
bernd_k
  • 12,369
  • 24
  • 79
  • 111
30
votes
7 answers

What are the minimum matching criteria recommended for reliable demographic based patient matching?

When matching patients based on demographic data are there any recommendations on what fields should match for the patient to be the "Same Patient"? I know the algorithms will be different for different implementations, I'm just curious if there are…
ScArcher2
30
votes
6 answers

Can I rely on reading SQL Server Identity values in order?

TL;DR: The question below boils down to: When inserting a row, is there a window of opportunity between the generation of a new Identity value and the locking of the corresponding row key in the clustered index, where an external observer could see…
Fabian Schmied
  • 597
  • 1
  • 5
  • 10
28
votes
3 answers

How to insert in a table with only an IDENTITY column?

Given a table with only an IDENTITY column, how do you insert a new row? I've tried the following: INSERT INTO TABLE (Syntax error) INSERT INTO TABLE VALUES() (Syntax error) INSERT INTO TABLE (Id) VALUES() (Syntax error) I am testing something…
Apocatastasis
  • 615
  • 2
  • 7
  • 15
26
votes
1 answer

Is it safe to rely on the order of an INSERT's OUTPUT clause?

Given this table: CREATE TABLE dbo.Target ( TargetId int identity(1, 1) NOT NULL, Color varchar(20) NOT NULL, Action varchar(10) NOT NULL, -- of course this should be normalized Code int NOT NULL, CONSTRAINT PK_Target PRIMARY KEY…
ErikE
  • 4,355
  • 4
  • 29
  • 39
20
votes
2 answers

Unexpected gaps in IDENTITY column

I'm trying to generate unique purchase order numbers that start at 1 and increment by 1. I have a PONumber table created using this script: CREATE TABLE [dbo].[PONumbers] ( [PONumberPK] [int] IDENTITY(1,1) NOT NULL, [NewPONo] [bit] NOT NULL, …
Ege Ersoz
  • 362
  • 1
  • 3
  • 10
19
votes
6 answers

What can be the downside of always having a single integer column as primary key?

Within one Web application I am working on, all database operations are abstracted using some generic repositories defined over Entity Framework ORM. However, in order to have a simple design for the generic repositories, all involved tables must…
Alexei
  • 1,191
  • 1
  • 14
  • 36
19
votes
4 answers

Changing identity column from INT to BIGINT

I have a table with an identity column that is also a primary key. Currently, it has 50 million rows, with the highest value of the identity column sitting at 148,921,803. The table has a lot of DELETEs and INSERTS performed on it, hence the high…
Felix Pamittan
  • 285
  • 1
  • 3
  • 12
18
votes
3 answers

Reset IDENTITY value

I have a table with an IDENTITY column. While developing I delete the rows from time to time and add them again. But the IDENTITY values always kept increasing and didn't start from 1 when I added them again. Now my id's go from 68 -> 92 and this…
Gijs
  • 333
  • 1
  • 2
  • 7
16
votes
3 answers

What are Identity Columns?

I was reviewing the commit-fest scheduled for 7/01 for PostgreSQL and I saw that Pg is likely going to get "identity columns" sometime soon. I found some mention in information_schema.columns but nothing much is_identity yes_or_no …
Evan Carroll
  • 65,432
  • 50
  • 254
  • 507
16
votes
5 answers

Add autoincrement to existing PK

I created a table in a DB that already exists in another DB. It was initially populated with the old DB data. The table's PK had to receive the values that already exist on those records, so it couldn't be autoincrement. Now I need the new table to…
Hikari
  • 1,603
  • 12
  • 28
  • 42
15
votes
1 answer

Restarting identity columns in Postgresql

For serial columns used in Postgresql < 10, we manage the sequence by its name. We were able to reset a sequence with: SELECT setval('table_id_seq', (SELECT MAX(id) FROM table)); From version 10, using identity columns, there is no need to use the…
jgrocha
  • 395
  • 1
  • 6
  • 11
14
votes
3 answers

Why is SSMS inserting new rows at the top of a table not the bottom?

Whenever I manually insert a row into a table in SQL Server Management Studio 2008 (the database is SQL Server 2005) my new row appears at the TOP of the list rather than the bottom. I'm using identity columns and this results in things like id …
Zelda
  • 2,103
  • 6
  • 28
  • 38
1
2 3
13 14