How to add check constraint on one column using another column from same table

StackOverflow https://stackoverflow.com/questions/23308995

  •  09-07-2023
  •  | 
  •  

Domanda

My table structure is as follows:

create table Sale(
    id int primary key,
    product_ID int foreign key references ItemCategory.Item_Details(item_id),
    product_name varchar(10),
    unit_price int,
    quantity int,
    discount int,
    total_price int,
    profit int
)  

Now I have to implement the following constraints in the sale table: The discount offered on a product should not be greater than 20% of the unit price of the product.

È stato utile?

Soluzione

ALTER TABLE Sale
  ADD CONSTRAINT ValidDiscount
  CHECK(discount <= unit_price *0.2);

This is ANSI 92, but IIRC not all RDBMS support it... MySQL doesn't.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top