I have an existing table that I am about to blow away because I did not create it with the ID
column set to be the table's Identity column.
Using SQL Server Management Studio, I scripted a "Create To..." of the existing table and got this:
CREATE TABLE [dbo].[History](
[ID] [int] NOT NULL,
[RequestID] [int] NOT NULL,
[EmployeeID] [varchar](50) NOT NULL,
[DateStamp] [datetime] NOT NULL,
CONSTRAINT [PK_History] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
My question is, how would I modify this SQL
so that my resulting table has the ID
column set as the Identity?
of course since you're creating the table in SQL Server Management Studio you could use the table designer to set the Identity Specification.
Unique key allows max 2 NULL values. Explaination:
If you try inserting same values as below:
Every time you will get error like:
This has already been answered, but I think the simplest syntax is:
The more complicated constraint index is useful when you actually want to change the options.
By the way, I prefer to name such a column HistoryId, so it matches the names of the columns in foreign key relationships.