I have a table where I want a constraint that checks the following (letters are column names, W is a 'bit' datatype):
If and only if 'W' = 0, check that X, Y & Z are unique.
Else, allow all new data.
I can do the 'W' is False
and the Check X, Y & Z are unique
separately, but merging the logic to allow for the above is what I can't figure out.
I tried
W = 1 OR UNIQUE (X, Y, Z)
but SSMS is saying that it's invalid.
Help is appreciated in advance :)
You cannot do that through a constraint. Instead, use a filtered unique index:
create unique index unq_x_w on t(x) where w = 0;
create unique index unq_y_w on t(y) where w = 0;
create unique index unq_z_w on t(z) where w = 0;
Actually, the above interprets your question as each column is unique. If you want the triple to be unique:
create unique index unq_xyz_w on t(x, y, z) where w = 0;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.