views:

2079

answers:

5

how do I make a composite key with SQL Server Management Studio?

I want two INT columns to form the identity (unique) for a table

+2  A: 

In design mode (right click table select modify) highlight both columns right click and choose set primary key

Gratzy
+1  A: 

Highlight both rows in the table design view and click on the key icon, they will now be a composite primary key.

I'm not sure of your question, but only one column per table may be an IDENTITY column, not both.

KM
+1  A: 
create table my_table (
    id_part1 int not null,
    id_part2 int not null,
    primary key (id_part1, id_part2)
)
Justice
+2  A: 

here is some code to do it:

-- Sample Table
create table myTable 
(
    Column1 int not null,
    Column2 int not null
)
GO

-- Add Constraint
ALTER TABLE myTable
    ADD CONSTRAINT pk_myConstraint PRIMARY KEY (Column1,Column2)
GO

I added the constraint as a separate statement because I presume your table has already been created.

Roatin Marth
I guess this answer fits since you can run this within Query window of SSMS... ;)
Sung Meister
+7  A: 
Cory
I wish I could up-vote twice - the image is a nice touch.
CJM
Thanks a lot, appreciate the comment
Cory