Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a primary key to a column that already exists.

Status
Not open for further replies.

mattyp75

Programmer
Aug 28, 2002
16
GB
How do I add a primary key to a column that already exists.
I've tried:
ALTER TABLE dbo.CensusSourceData_Ilot
ALTER COLUMN Column1
ADD CONSTRAINT Column1_pk PRIMARY KEY

but that doesn't work.

It seems a simple thing to do, but I can't see how to do it anywhere on the web. Any help would be much appreciated

thanks,
Matt
 
Code:
ALTER TABLE [dbo].[MyTable] WITH NOCHECK ADD 
	CONSTRAINT [PK_MyTable] PRIMARY KEY  CLUSTERED -- or NONCLUSTERED
	(
		[MyColumn]
	)  ON [PRIMARY] 
GO

HTH
 
Code:
alter table table_name
add constraint pk_name primary key (col_name)

--James
 
Thanks guys. Was starting to get a little frustrated there.
All is working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top