Auto increment primary key in SQL Server Management Studio 2012

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

    I had this issue where I had already created the table and could not change it without dropping the table so what I did was: (Not sure when they implemented this but had it in SQL 2016)

    Right click on the table in the Object Explorer:

    Script Table as > DROP And CREATE To > New Query Editor Window

    Then do the edit to the script said by Josien; scroll to the bottom where the CREATE TABLE is, find your Primary Key and append IDENTITY(1,1) to the end before the comma. Run script.

    The DROP and CREATE script was also helpful for me because of this issue. (Which the generated script handles.)

提交回复
热议问题