Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add unique constraint depending on value in SQL

Tags:

sql

sql-server

i have a situation where i want to add unique constaint only when there is a certain value in other field for example the table is ID CategoryName Name Value CategoryID

the constraint will be on ID, CategoryName and Name only is CategoryID is 0

is it possible?

like image 847
li-raz Avatar asked Mar 28 '26 22:03

li-raz


1 Answers

CREATE UNIQUE NONCLUSTERED INDEX [UQ_ID_CategoryName_Name, CategoryName, Name] ON TableName 
(
    ID, CategoryName, Name
)
WHERE (CategoryID = 0)
like image 136
Khan Avatar answered Apr 01 '26 06:04

Khan



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!