The values of one column cannot be greater than another

前端 未结 1 469
一向
一向 2021-02-15 16:20

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          


        
1条回答
  •  感动是毒
    2021-02-15 16:49

    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);
    

    0 讨论(0)
提交回复
热议问题