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 danielledunham on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding primary key to table

Status
Not open for further replies.

RebLazer

Programmer
Joined
Jun 7, 2002
Messages
438
Location
US
I need to add a primary key on column name "primary_key" to an already-existing table "dbo.select3rd". I know I need to use the "alter table" command but I can't get the syntax right.

Any help would be very appreciated!
Thanks!
Lazer
 
ALTER TABLE [dbo].[select3rd] WITH NOCHECK ADD
CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
(
[primary_key]
) ON [PRIMARY]

Thanks

J. Kusch
 
Try this (Suggest test environment)

CREATE TABLE test ( column_a INT)

ALTER TABLE test ADD
/* Add a PRIMARY KEY */
PK_Column varchar(20)
CONSTRAINT column_b_pk PRIMARY KEY
/* Add a default value */
CONSTRAINT AddDflt
DEFAULT 1 WITH VALUES

Tested this in northwind db
Hope this helps
Have fun!

[flowerface]

The pen is mightier than the sword - and quite frankly . . easier to write with
 
Thank you very much, too, theB______!

:-)
Lazer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top