Sub topic 14
Constraint States: ENABLE, DISABLE, VALIDATE, and DEFERRABLE
This chapter assumes that you know basic CREATE TABLE, INSERT, and ALTER TABLE statements. It also assumes an Oracle Database 12c or later schema and the employees table from the earlier examples.
A constraint has two related concerns:
- Whether Oracle enforces it for new data.
- Whether existing data has already been checked.
Understanding both concerns helps you load data safely, repair legacy data, and control when related changes are checked.
ENABLE and DISABLE
An enabled constraint is enforced. Oracle checks new INSERT and UPDATE statements against it. A foreign key also affects which parent rows can be deleted.
A disabled constraint is not enforced. Existing rows remain in the table, even if they violate the disabled rule. Disabling a constraint can be useful during a controlled data load, but it creates a period when invalid data can enter the table.
Use the constraint name when changing its state:
ALTER TABLE employees
DISABLE CONSTRAINT employees_manager_fk;
ALTER TABLE employees
ENABLE CONSTRAINT employees_manager_fk;
ALTER TABLE employees
DROP CONSTRAINT employees_manager_fk;Enabling a constraint normally checks existing rows. If any row violates the rule, the ENABLE statement fails and the constraint remains unusable in its requested state.
VALIDATE and NOVALIDATE
VALIDATE and NOVALIDATE describe existing data.
| State | New DML checked? | Existing rows checked? |
|---|---|---|
| ENABLE VALIDATE | Yes | Yes |
| ENABLE NOVALIDATE | Yes | No |
ENABLE NOVALIDATE is useful when a table contains old violations that you plan to repair later. Oracle checks all new changes, while leaving old rows alone.
ALTER TABLE employees
ENABLE NOVALIDATE CONSTRAINT employees_salary_ck;
-- Later, after correcting all invalid salaries:
ALTER TABLE employees
ENABLE VALIDATE CONSTRAINT employees_salary_ck;The second statement performs the full validation. It fails if existing data still violates the check constraint.
You can also disable a constraint and later enable it without immediately validating old rows:
ALTER TABLE employees
DISABLE CONSTRAINT employees_dept_fk;
ALTER TABLE employees
ENABLE NOVALIDATE CONSTRAINT employees_dept_fk;Use this approach deliberately. NOVALIDATE does not make existing data correct. It only limits what Oracle checks at that moment.
DEFERRABLE constraints
Most constraints are checked when each statement runs. A deferrable constraint can postpone its check until the transaction ends. This helps when several statements temporarily create an inconsistent relationship but produce valid final data.
Create a deferrable constraint when the table is created:
CREATE TABLE project_tasks (
task_id NUMBER PRIMARY KEY,
predecessor_id NUMBER,
task_name VARCHAR2(100) NOT NULL,
CONSTRAINT project_tasks_predecessor_fk
FOREIGN KEY (predecessor_id)
REFERENCES project_tasks (task_id)
DEFERRABLE INITIALLY DEFERRED
);INITIALLY DEFERRED means Oracle checks the constraint at COMMIT. You can change the timing for the current transaction:
SET CONSTRAINTS project_tasks_predecessor_fk IMMEDIATE;
SET CONSTRAINTS project_tasks_predecessor_fk DEFERRED;INITIALLY IMMEDIATE is the default behavior for a deferrable constraint. It checks each statement unless you defer it explicitly.
Oracle cannot make every rule deferrable. In particular, NOT NULL constraints are checked immediately. Deferrability is mainly useful for primary key, unique, and foreign key constraints.
Choosing a state
The following flow summarizes a practical decision process.
Exercise
Exercise
You need Oracle to reject new invalid foreign-key values, but existing rows may still contain violations. Which state should you use, and when should you use ENABLE VALIDATE?
Show answer
Use ENABLE NOVALIDATE first. After repairing the existing violations, use ENABLE VALIDATE to confirm the entire table satisfies the constraint.