You made syntax errors.
Seeing your requirements, ALTER statement might be .....
Code:
ALTER TABLE data_value
ADD CONSTRAINT validate_value
CHECK(
(conditions for value)
AND value
BETWEEN (SELECT SUM(value) FROM data_value WHERE ...)
AND (SELECT SUM(value) FROM data_value WHERE ...)
)
or
Code:
ALTER TABLE data_value
ADD CONSTRAINT validate_value
CHECK(
NOT (conditions for value)
OR
value
BETWEEN (SELECT SUM(value) FROM data_value WHERE ...)
AND (SELECT SUM(value) FROM data_value WHERE ...)
)
But, subqueries cannot be included in a CHECK constraint.
See, CREATE TABLE statement in DB2 SQL Reference.
Quote:
CHECK (check-condition)
Defines a check constraint. The search-condition must be true or unknown for every row of the table.
search-condition The search-condition has the following restrictions:
v A column reference must be to a column of the table being created.
v The search-condition cannot contain a TYPE predicate.
v The search-condition cannot contain any of the following (SQLSTATE 42621): – Subqueries
– XMLQUERY or XMLEXISTS expressions
– Dereference operations or DEREF functions where the scoped reference argument is other than the object identifier (OID) column
.....
.....
|
I thought that an alternative way is CREATE TRIGGER(s).