Despite the fact that SQL Server permits the addition of multiple check constraints on a single column that is referenced by other columns in the same table, attempts to write the syntax in the following way frequently result in errors.
CREATE TABLE tbProducts(
ProductID INT IDENTITY PRIMARY KEY,
ProductName VARCHAR(255) NOT NULL,
PurchasedPrice DECIMAL(10,2),
SalablePrice DECIMAL(10,2),
DiscountedPrice DECIMAL(10,2) CHECK (DiscountedPrice > PurchasedPrice),
CHECK (DiscountedPrice < SalablePrice)
);
The error is as shown below:
It is because the syntax is for a column-level constraint, so it can only be used with the column to which it is connected. Instead, consider naming the constraints at the table level, as demonstrated below.
CREATE TABLE tbProducts(
ProductID INT IDENTITY PRIMARY KEY,
ProductName VARCHAR(255) NOT NULL,
PurchasedPrice DECIMAL(10,2),
SalablePrice DECIMAL(10,2),
DiscountedPrice DECIMAL(10,2),
CONSTRAINT Ck_DiscountedPrice_tbProducts1 CHECK (DiscountedPrice > PurchasedPrice),
CONSTRAINT Ck_DiscountedPrice_tbProducts2 CHECK (DiscountedPrice < SalablePrice)
);
This also motivates you to avoid using the awful system defaults and give your constraints meaningful names.
Hope this helps.