Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: how to formulate a complex constraint with case when

The following conditional constraint simply doesn't work. Is it possible to formulate in a working way?

ALTER TABLE eni_trasc_voci_pwr_fatt
ADD CONSTRAINT tvp_constraint_1 CHECK (
CASE WHEN TVP_CODICE_ASSOGGETAMEN = '-' THEN tvp_regione IS NULL
END);
like image 528
Revious Avatar asked Dec 20 '22 21:12

Revious


2 Answers

Try the following:

ALTER TABLE eni_trasc_voci_pwr_fatt
ADD CONSTRAINT tvp_constraint_1 CHECK (
CASE WHEN TVP_CODICE_ASSOGGETAMEN = '-' THEN tvp_regione else null end IS NULL);
like image 151
int2000 Avatar answered Apr 06 '23 16:04

int2000


It looks like you want logical implication here ("if X then Y"), which is logically equivalent to "(not X) or Y". CASE is used to create a finite map.

Your constraint should be something like

TVP_CODICE_ASSOGGETAMEN != '-' OR TVP_REGIONE IS NULL

like image 36
Judge Mental Avatar answered Apr 06 '23 17:04

Judge Mental