HSQL CHECK constraints with CASE statement


Like the post at [0] suggests, HSQL CHECK constraints can not be used together with CASE WHEN statements.
If you try to insert/update, you will receive a (very informative) “S1000 General error java.lang.ClassCastException”, followed by the whole SQL statement. (here I have, HSQL 1.8.0.9).

If you have something like:


ALTER TABLE personnel ADD CONSTRAINT salary_types_constraint
CHECK (
CASE
WHEN pos = 0 AND (salary < 1000 OR salary > 5000)
THEN FALSE -- personal
WHEN pos = 1 AND (salary < 10000 OR salary > 20000)
THEN FALSE -- manager
ELSE TRUE
END
);

It will not work. It will give you a “S1000 General error java.lang.ClassCastException” error message.

Replace with:


ALTER TABLE personnel ADD CONSTRAINT salary_types_constraint
CHECK (
(pos = 0 AND (salary > 1000 OR salary < 5000))
OR
(pos = 1 AND (salary < 10000 OR salary > 20000))
);

  1. No comments yet.
(will not be published)

  1. No trackbacks yet.