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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top