CHECK CONSTRAINT in Oracle SQL -


i have following table goods_in_wagon(goods_id,wagon_id,total_weight). need create number of if statement check constraint says

"if wagon_id between 90 , 99 total_weight must greater 10." , "if wagon_id between 100 , 110 total_weight must greater 20." , "if wagon_id between 111 , 120 total_weight must greater 30."

is possible in sql oracle, if how can implement

use out-of-line constraint:

create table goods_in_wagon (   goods_id number(whatever),   wagon_id number(whatever),   total_weight number(whatever),   constraint check_wagid_weight     check (wagon_id not between 90 , 99 or total_weight > 10) ) 

if wagon_id not between 90 , 99, constraint passes. if between 90 , 99, total_weight must greater 10.

an out-of-line constraint allows apply constraint logic @ row level, meaning can use of column values.


addendum here's how handle updated question ranges of wagon_id , total_weight. there other ways felt "cleanest", meaning easiest me read :)

create table goods_in_wagon(   goods_id number(whatever),   wagon_id number(whatever),   total_weight number(whatever),   constraint check_wagid_weight     check (       (wagon_id < 90) or       (wagon_id between 90 , 99 , total_weight > 10) or       (wagon_id between 100 , 110 , total_weight > 20) or       (wagon_id between 111 , 120 , total_weight > 30) or       (wagon_id > 120)     ) ) 

Comments