Is there a way in SQL Server to create a table with a primary key that auto-increments itself? I’ve been looking at the “UniqueIdentifier” type, but this doesn’t seem to do what I expect.
Currently, I have something like this:
CREATE TABLE [dbo].[MyTable](
[Date] [datetime] NOT NULL,
[MyField1] [nchar](50) NOT NULL,
[MyField2] [nvarchar](max) NULL,
[Key] [uniqueidentifier] NOT NULL
) ON [PRIMARY]
Basically, I want KEY to start at 1 and just increment itself for each record.
You’re looking for the IDENTITY property.
From the documentation:
seed
increment
Btw, all of this is also easily achieved from Sql Server’s mgmt UI. Just right click on your table and select design. Then select the proper column and set the IDENTITY property.