Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 add identity primary key

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2014-06-03 : 05:01:12
Hi

I need to add a identity, primary key with auto increment on a exsisting table, what would be the correct syntax to do that?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-06-03 : 08:01:04
[code]ALTER TABLE dbo.YourTable
ADD RowID INT NOT NULL IDENTITY(1, 1);
GO

ALTER TABLE dbo.YourTable
ADD CONSTRAINT PK_YourTable
PRIMARY KEY NONCLUSTERED
(
RowID
)
WITH (
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
);
GO[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2014-06-03 : 09:22:37
Excellent, thanks Peso!
Go to Top of Page
   

- Advertisement -