Oracle Check Constraint Oracle Check Constraint oracle oracle

Oracle Check Constraint


While I do not have Oracle, I did a quick test with PostgreSQL and your first example (IS_DISABLED being NULL and DISABILITY_INCOME_TYPE_ID being 1):

postgres=> select (null is null and 1 is null); ?column?---------- f(1 registro)postgres=> select (null is null and 1 is null) or (null = 0 and 1 is null); ?column?---------- f(1 registro)postgres=> select (null is null and 1 is null) or (null = 0 and 1 is null) or (null = 1); ?column?----------(1 registro)

Here we see clearly that, in this case, your expression (at least on PostgreSQL) returns NULL. From the manual,

[...] Expressions evaluating to TRUE or UNKNOWN succeed. Should any row of an insert or update operation produce a FALSE result an error exception is raised and the insert or update does not alter the database. [...]

So, if Oracle behaves the same as PostgreSQL, the check constraint would pass.

To see if this is the case, avoid the NULL shenanigans by explicily checking for it and see if it works:

CHECK ((IS_DISABLED IS NULL AND DISABILITY_INCOME_TYPE_ID IS NULL)    OR (IS_DISABLED IS NOT NULL AND IS_DISABLED = 0 AND DISABILITY_INCOME_TYPE_ID IS NULL)    OR (IS_DISABLED IS NOT NULL AND IS_DISABLED = 1));


Try using NVL in the check condition.


I'm not sure why the compound check isn't working, but this works:

ALTER TABLE CLIENTS ADD CONSTRAINT CHK_1 CHECK (IS_DISABLED = 0 AND DISABILITY_INCOME_TYPE_ID IS NULL)ALTER TABLE CLIENTS ADD CONSTRAINT CHK_2 CHECK (IS_DISABLED IS NULL AND DISABILITY_INCOME_TYPE_ID IS NULL)ALTER TABLE CLIENTS ADD CONSTRAINT CHK_3 CHECK (IS_DISABLED = 1)

RegardsK