You receive an ORA-02293 error because you already have data in your table that does not satisfy your newly created validation constraint.
I think the important part here is to understand that you do not have one business rule to check here, but three. And it would be very convenient for the user who checks the restriction to know EXACTLY what is wrong with the line that he inserts. Therefore, why not go for these restrictions:
SQL> create table room (id,type,price) 2 as 3 select 1, 'S', 50 from dual union all 4 select 2, 'D', 80 from dual union all 5 select 3, 'F', 110 from dual 6 / Table created. SQL> alter table room add constraint single_room_below_50 check (type != 'S' or price <= 50) 2 / Table altered. SQL> alter table room add constraint double_room_below_100 check (type != 'D' or price <= 100) 2 / Table altered. SQL> alter table room add constraint family_room_below_150 check (type != 'F' or price <= 150) 2 / Table altered. SQL> insert into room values (4, 'S', 60) 2 / insert into room values (4, 'S', 60) * ERROR at line 1: ORA-02290: check constraint (RWIJK.SINGLE_ROOM_BELOW_50) violated
Yours faithfully,
Rob
PS: More information on this topic can be found in this blog post box .
source share