SQL Server > Table

Add Auto-increment and primary key in SQL Server

How to create a new column in the existing database table as Auto increment and primary key?


To create a new column in the existing database table as primary key and auto increment, execute following script in the query window against the database.

Alter table PersonalDetails
Add AutoId int NOT NULL IDENTITY (1, 1) Primary key

This adds a new column as AutoId with auto increment value and set this column as Primary key of the table.

IDENTITY (1, 1) means the start value of the AutoId column value will be 1 and increment by 1 for every new record.

 Views: 14787 | Post Order: 22



Write for us






Hosting Recommendations