Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server unique key for each partition

I have a table that contain 3 columns:

ID int [primary key]

BranchRef int [foreign key]

Code int

I need to [Code] be unique in each [BranchRef]

like:

ID      BranchRef      Code
1           1            1
2           1            2
3           2            1
4           2            2
5           1            3

I really appreciate any help

like image 676
Pouria Sharif Avatar asked Feb 25 '26 01:02

Pouria Sharif


1 Answers

Add a unique constraint.

ALTER TABLE mytable ADD CONSTRAINT UNIQUE(BranchRef, Code);
like image 126
wvdz Avatar answered Feb 28 '26 07:02

wvdz



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!