The CHECK constraint enforces
column value restrictions. Such constraints can restrict a column,
for example, to a set of values, only positive numbers, or reasonable
dates. Figure shows an example of CHECK
constraints using a modified version of the friend table from
Figure
, page
.
test=> CREATE TABLE friend2 (
test(> firstname CHAR(15),
test(> lastname CHAR(20),
test(> city CHAR(15),
test(> state CHAR(2) CHECK (length(trim(state)) = 2),
test(> age INTEGER CHECK (age >= 0),
test(> gender CHAR(1) CHECK (gender IN ('M','F')),
test(> last_met DATE CHECK (last_met BETWEEN '1950-01-01'
test(> AND CURRENT_DATE),
test(> CHECK (upper(trim(firstname)) != 'ED' OR
test(> upper(trim(lastname)) != 'RIVERS')
test(> );
CREATE
test=> INSERT INTO friend2
test-> VALUES ('Ed', 'Rivers', 'Wibbleville', 'J', -35, 'S', '1931-09-23');
ERROR: ExecAppend: rejected due to CHECK constraint friend2_last_met
This figure has many CHECK clauses: