I have a table called Room and it has columns (ID, type, price…etc)
I want to add constraints for both type and price like:
- if single (s), then price should not be greater than 50,
- if double (d), then price should not be greater than 100, and
- if family (f), then price should not be greater than 150
I tried to add it like this but it’s giving me an error. Not sure how should I write this:
ALTER TABLE ROOM
ADD (CONSTRAINT CHK_PRICE CHECK (
(TYPE='S' AND PRICE <= 50) AND
(TYPE='D' AND PRICE <=100) AND
(TYPE='F' AND PRICE <= 150)));
The error received is:
SQL Error: ORA-02293: cannot validate (xxxx.CHK_PRICE) - check
constraint violated
02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
*Cause: an alter table operation tried to validate a check constraint to
populated table that had nocomplying values.
*Action: Obvious
It sounds like you need to
ORtogether the three conditions, notANDthem together. It is impossible for any row to satisfy all three criteria–typecannot simultaneously have a value of S, D, and F. You probably want