joshua0823
joshua0823

Reputation: 390

Adding a primary key in SQL Server 2005 not working

I am pretty new to database stuff in general and I can't seem to get any sample code for creating a primary key to work. I am using Microsoft SQL Server and the server type is SQL Server 2005 (90). The code I am currently trying to use is:

ALTER TABLE dbo.CustomerVisit
ALTER COLUMN CustomerID int NOT NULL;
ADD CONSTRAINT PK_CustomerVisit PRIMARY KEY CLUSTERED (CustomerID)
GO

But I am getting an error:

Incorrect Syntax near the keyword 'CONSTRAINT'

I just created this table and it has no constraints or anything. Just 3 columns. I've also tried

ADD PRIMARY KEY CustomerID;

but that results in

Incorrect Syntax new the keyword 'PRIMARY'

Upvotes: 0

Views: 274

Answers (1)

Andomar
Andomar

Reputation: 238078

add constraint comes after alter table. The ; ends the previous alter table, so you have to start the new statement with alter table again:

ALTER TABLE dbo.CustomerVisit ADD CONSTRAINT PK_CustomerVisit 
    PRIMARY KEY CLUSTERED (CustomerID);

Upvotes: 1

Related Questions