Sub topic 7
Validating Values with CHECK
This chapter assumes that you know basic SELECT, INSERT, UPDATE, and DELETE statements. You should also understand tables, rows, columns, and keys. A CHECK constraint is a rule that Oracle tests whenever a row is inserted or updated.
What a CHECK constraint does
A CHECK constraint limits a column or a group of columns to valid values. The condition must not be false. If the condition is false, Oracle rejects the statement with an error.
Name each constraint explicitly. A useful name makes errors easier to understand and lets you manage the constraint later.
CREATE TABLE employee_pay (
employee_id NUMBER PRIMARY KEY,
monthly_salary NUMBER(10,2) NOT NULL,
bonus NUMBER(10,2),
status VARCHAR2(10) DEFAULT 'ACTIVE' NOT NULL,
start_date DATE NOT NULL,
end_date DATE,
CONSTRAINT employee_pay_salary_ck
CHECK (monthly_salary >= 0),
CONSTRAINT employee_pay_bonus_ck
CHECK (bonus >= 0),
CONSTRAINT employee_pay_status_ck
CHECK (status IN ('ACTIVE', 'INACTIVE')),
CONSTRAINT employee_pay_dates_ck
CHECK (end_date IS NULL OR end_date >= start_date)
);The PRIMARY KEY constraint is written inline. The named CHECK constraints are written out of line after the column definitions. Out-of-line constraints are useful when a rule refers to more than one column, such as end_date and start_date.
NULL and three-valued logic
Oracle evaluates a CHECK condition as true, false, or unknown. A row is accepted when the condition is true or unknown. Therefore, a CHECK constraint does not automatically require a value.
| Condition | Value | Result |
|---|---|---|
| `bonus >= 0` | `50` | Accepted |
| `bonus >= 0` | `-10` | Rejected |
| `bonus >= 0` | `NULL` | Accepted because the result is unknown |
Use NOT NULL as well when a value is required. In the example, monthly_salary cannot be null because its column definition includes NOT NULL. The bonus column can be null, so its check allows a missing bonus.
A default value is applied when an INSERT statement omits the column. The resulting value must still satisfy its CHECK constraint. An explicitly supplied value does not use the default.
Common CHECK rules and limits
Use IN for a small fixed list of permitted values. Use comparisons for ranges. Use expressions involving multiple columns for relationships within the same row.
A CHECK constraint can enforce rules such as:
quantity > 0status IN ('NEW', 'PAID', 'CANCELLED')discount BETWEEN 0 AND 100end_date IS NULL OR end_date >= start_date
A CHECK constraint should describe the current row. It cannot use a subquery to inspect another table or another row. Use a FOREIGN KEY for a relationship between tables, and use a UNIQUE constraint when a value must not repeat. More complex cross-row rules may require a different database design or controlled application logic.
Exercise
Write a named constraint for a required PRIORITY column that accepts only the numbers 1 through 5.
Show answer
Use priority NUMBER NOT NULL with CONSTRAINT task_priority_ck CHECK (priority BETWEEN 1 AND 5).