Search code examples
sql-servert-sqlprimary-keyauto-increment

how to add primary key column with auto increment in sql server


i have one existing table in that i need to add primary column with auto increment(1,1). how to write query for to insert one primary key column with identity(1,1). getting error -

"Incorrect syntax near the keyword 'IDENTITY".

table ALTER TABLE OLTMS_0B8DF2
        ADD PRIMARY KEY (ID);

i tried like this

ALTER TABLE OLTMS_0B8DF2
ADD PRIMARY KEY (ID) int IDENTITY(1,1);

getting error


Solution

  • You should use inline constraint syntax

    ALTER TABLE OLTMS_0B8DF2
    ADD ID INT IDENTITY(1,1) PRIMARY KEY