61

I have a table set up that currently has no primary key. All I need to do is add a primary key, no null, auto_increment.

I'm working with a Microsoft SQL Server database. I understand that it can't be done in a single command but every command I try keeps returning syntax errors.

edit ---------------

I have created the primary key and even set it as not null. However, I can't set up the auto_increment.

I've tried:

ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment
ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment

I'm using NVARCHAR because it wouldn't let me set NOT NULL under int

DineshDB
  • 5,998
  • 7
  • 33
  • 49
dcp3450
  • 10,959
  • 23
  • 58
  • 110

6 Answers6

118

It can be done in a single command. You need to set the IDENTITY property for "auto number":

ALTER TABLE MyTable ADD mytableID int NOT NULL IDENTITY (1,1) PRIMARY KEY

More precisely, to set a named table level constraint:

ALTER TABLE MyTable
   ADD MytableID int NOT NULL IDENTITY (1,1),
   CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (MyTableID)

See ALTER TABLE and IDENTITY on MSDN

adam.wulf
  • 2,149
  • 20
  • 27
gbn
  • 422,506
  • 82
  • 585
  • 676
  • 1
    PERFECT! thanks for your help. I'll accept the answer once the timer allows me to. :D – dcp3450 Jul 21 '11 at 14:39
  • 6
    Is OK, but the correct syntax is without the second "ADD", it is: ALTER TABLE MyTable ADD MytableID int NOT NULL IDENTITY (1,1), CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (MyTableID) – Daniel Silva Aug 17 '16 at 18:38
21

If the table already contains data and you want to change one of the columns to identity:

First create a new table that has the same columns and specify the primary key-kolumn:

create table TempTable
(
    Id int not null identity(1, 1) primary key
    --, Other columns...
)

Then copy all rows from the original table to the new table using a standard insert-statement.

Then drop the original table.

And finally rename TempTable to whatever you want using sp_rename:

http://msdn.microsoft.com/en-us/library/ms188351.aspx

Richard Barker
  • 1,161
  • 2
  • 12
  • 30
Andreas Ågren
  • 3,879
  • 24
  • 33
  • this create table syntax is **good practice** as advised by author of [this](http://stackoverflow.com/a/4862427/2218697) post in comments, hope helps someone. – Shaiju T Nov 01 '15 at 11:22
13

You can also perform this action via SQL Server Management Studio.

Right click on your selected table -> Modify

Right click on the field you want to set as PK --> Set Primary Key

Under Column Properties set "Identity Specification" to Yes, then specify the starting value and increment value.

Then in the future if you want to be able to just script this kind of thing out you can right click on the table you just modified and select

"SCRIPT TABLE AS" --> CREATE TO

so that you can see for yourself the correct syntax to perform this action.

Community
  • 1
  • 1
ChrisCamp
  • 672
  • 1
  • 5
  • 20
1

If you have the column it's very easy.

Using the designer, you could set the column as an identity (1,1): right click on the table → design → in part left (right click) → properties → in identity columns, select #column.


Properties:

enter image description here

Identity column:

enter image description here

TT.
  • 15,774
  • 6
  • 47
  • 88
0

In SQL Server 2008:

  • Right click on table
  • Go to design
  • Select numeric datatype
  • Add Name to the new column
  • Make Identity Specification to 'YES'
-1

you can try this...

ALTER TABLE Your_Table
 ADD table_ID int NOT NULL PRIMARY KEY auto_increment;
RF1991
  • 2,037
  • 4
  • 8
  • 17
  • 1
    Thanks for your answer. However, it would be helpful if you could provide a brief explanation of your code and how it solves the problem. This will make your answer more informative and easier to understand for future readers. – Houssem Apr 26 '23 at 10:07