Auto increment primary key in SQL Server Management Studio 2012

前端 未结 11 1273
谎友^
谎友^ 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:55

    When you're creating the table, you can create an IDENTITY column as follows:

    CREATE TABLE (
      ID_column INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
      ...
    );
    

    The IDENTITY property will auto-increment the column up from number 1. (Note that the data type of the column has to be an integer.) If you want to add this to an existing column, use an ALTER TABLE command.

    Edit:
    Tested a bit, and I can't find a way to change the Identity properties via the Column Properties window for various tables. I guess if you want to make a column an identity column, you HAVE to use an ALTER TABLE command.

提交回复
热议问题