0

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.

4
  • You should define a table constraint, not a column constraint. Commented Apr 26, 2014 at 9:28
  • @Frazz but the scenario is first the table is created after that i have to alter the table to add the constraint. How to do? Commented Apr 26, 2014 at 9:36
  • You must define discount as percent not as absolute value. In this case you can set check constraint on discount field only. Notice, that setting discount as absolute value creates functional dependency of on part of row to another part which breaks normalization rules. Commented Apr 26, 2014 at 9:37
  • can any one give me the example how can i achieve this... Commented Apr 26, 2014 at 9:39

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.