Create auto increment field in SQL Server

Categoría
Description

Sometimes you may need to create an auto increment field for using as identifier in a table.

SQL Server uses IDENTITY type to create an auto increment field:

IDENTITY (<begin>, <increment>)

Where:

  • <begin> is the number to begin with
  • <incremento> means the increment value

For example:

CREATE TABLE Persona
   (
      ID int IDENTITY(1,1) PRIMARY KEY,
      FirstName varchar(255),
      Surname varchar(255)
   )

Will create a Persona table, with an ID Primary Key, which will be int type. It will begin counting on 1 and will increment 1 by 1.