Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a auto_increment primary key in SQL Server database?

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

like image 285
dcp3450 Avatar asked Jul 21 '11 14:07

dcp3450


People also ask

Can a primary key be AUTO_INCREMENT?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

How do I add an auto increment to an existing table?

To add a new AUTO_INCREMENT integer column named c : ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (c); We indexed c (as a PRIMARY KEY ) because AUTO_INCREMENT columns must be indexed, and we declare c as NOT NULL because primary key columns cannot be NULL .

How do I change my primary key to auto increment?

To change a primary key to auto_increment, you can use MODIFY command. Let us first create a table. Look at the above sample output, StudentId column has been changed to auto_increment.

What is AUTO_INCREMENT in SQL?

The auto increment in SQL is a feature that is applied to a field so that it can automatically generate and provide a unique value to every record that you enter into an SQL table. This field is often used as the PRIMARY KEY column, where you need to provide a unique value for every record you add.


2 Answers

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

like image 128
gbn Avatar answered Oct 06 '22 23:10

gbn


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

like image 33
Andreas Ågren Avatar answered Oct 06 '22 23:10

Andreas Ågren