Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Unique Combination check constraint [duplicate]

Possible Duplicate:
Unique Constraint, excluding NULL values

Lets say i have a table with addresses, and i want an owner to have only One main address. Fist do i define a index/key or a check constraint? What's the expression for this?

TABLE_Address
--OwnerId
--IsMain    (not null able)
--City etc....

So i need a Unique combination of ( IsMain ==true AND ownerId)

like image 869
Anestis Kivranoglou Avatar asked Dec 20 '25 13:12

Anestis Kivranoglou


1 Answers

Create a filtered unique index on ownerId with condition IsMain = 1. This index will a) enforce this condition and b) help your application to the pre-insert validation query.

like image 164
usr Avatar answered Dec 22 '25 04:12

usr



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!