Auto increment primary key in SQL Server Management Studio 2012

前端 未结 11 1207
谎友^
谎友^ 2020-11-22 10:11

How do I auto increment the primary key in a SQL Server database table, I\'ve had a look through the forum but can\'t see how.

11条回答
  •  一生所求
    2020-11-22 10:41

    CREATE TABLE Persons (
        Personid int IDENTITY(1,1) PRIMARY KEY,
        LastName varchar(255) NOT NULL,
        FirstName varchar(255),
        Age int
    );
    

    The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.

    In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.

    Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5).

    To insert a new record into the "Persons" table, we will NOT have to specify a value for the "Personid" column (a unique value will be added automatically):

提交回复
热议问题