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

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


enter image description here

  1. Open the design table tab
  2. Highlight your two INT fields (Ctrl/Shift+click on the grey blocks in the very first column)
  3. Right click -> Set primary key


here is some code to do it:

-- Sample Tablecreate table myTable (    Column1 int not null,    Column2 int not null)GO-- Add ConstraintALTER 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.


create table my_table (    id_part1 int not null,    id_part2 int not null,    primary key (id_part1, id_part2))