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 2000 Forums
 Transact-SQL (2000)
 Asigning Primary Key to existing column

Author  Topic 

WindChaser
Posting Yak Master

225 Posts

Posted - 2004-10-20 : 23:21:54

Hi,

I have a table containing an ID column which is already set to have Identity(1,1), Not Null. Now, I'd like to assign it the Primary Key. I've tried just about every which way. What's wrong with the following statement syntax:

Alter Table Mytable Alter column ID int Not Null CONSTRAINT PRIMARY KEY

Thanks!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-10-20 : 23:34:54
Try this:

ALTER TABLE myTable ADD CONSTRAINT PK_myTable PRIMARY KEY(ID)

ALTER COLUMN only works to change the data type, size, and/or nullability of the column. Constraints and defaults are added or dropped according to the syntax above. Books Online has a good example under "ALTER TABLE", but it's kinda buried.
Go to Top of Page

WindChaser
Posting Yak Master

225 Posts

Posted - 2004-10-21 : 09:26:59

Thanks robvolk! I spent hours last evening trying to figure it out from Books Online. Mind you, I was accessing the SQLServer7 BO. Maybe the newest version contains better examples.
Go to Top of Page
   

- Advertisement -