Skip to content Skip to sidebar Skip to footer

The Values Of One Column Cannot Be Greater Than Another

I am trying to create a table where the values in one column can't be greater than the next column over. For example, I am creating the following table. CREATE TABLE Price ( Pr

Solution 1:

Just change it to a table-level constraint instead of a column constraint.

CREATE TABLE Price (
    PriceID INT PRIMARY KEY IDENTITY (1,1),
    OriginalPrice FLOAT NOT NULL,
    CurrentPrice FLOAT NOT NULL,
    Discount FLOAT,
    ShippingCost FLOAT NOT NULL,
    Tax FLOAT NOT NULL,
    CHECK (CurrentPrice <= OriginalPrice));

You can also add it after, e.g.

ALTER TABLE Price ADD CHECK (CurrentPrice <= OriginalPrice);
--or
ALTER TABLE Price ADD CONSTRAINT CK_Price_Current_vs_Original
    CHECK (CurrentPrice <= OriginalPrice);

Post a Comment for "The Values Of One Column Cannot Be Greater Than Another"