Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a composite key without making it a primary key in SQL Server ?

How to make a composite key without making it a primary key in SQL Server ?

I have two int columns in a table and I want to make them as composite key, but sadly I don't want them to set as primary key (Selecting both columns and select primary key in SSMS).

Is it possible ?

like image 962
shrekDeep Avatar asked Oct 21 '25 02:10

shrekDeep


1 Answers

What do you mean by making them composite key ?

Do you want to make them unique (in combination)?

Just create a UNIQUE CONSTRAINT on them:

ALTER TABLE dbo.YourTable
ADD CONSTRAINT UC_YourTable_Col1_Col2
UNIQUE(Col1, Col2)

Or what else do you want to achieve by making them composite key ?

like image 160
marc_s Avatar answered Oct 23 '25 17:10

marc_s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!