Sub topic 12
Adding and Dropping Constraints with `ALTER TABLE`
This chapter assumes that you know basic CREATE TABLE and INSERT syntax, and that an employees table already exists. You will learn how to change its constraints without recreating the table.
A constraint is a rule that protects the data in a table. Oracle lets you add, disable, enable, and drop constraints with ALTER TABLE. Name each constraint explicitly so that you can manage it later.
Adding constraints to an existing table
Use ADD CONSTRAINT to add a named primary key, unique constraint, check constraint, or foreign key.
ALTER TABLE employees
ADD CONSTRAINT employees_email_uk UNIQUE (email);
ALTER TABLE employees
ADD CONSTRAINT employees_salary_ck CHECK (salary >= 0);
ALTER TABLE employees
ADD CONSTRAINT employees_dept_fk
FOREIGN KEY (department_id)
REFERENCES departments (department_id)
ON DELETE SET NULL;Oracle checks existing rows when it enables a new constraint. If any existing row violates the rule, the statement fails. For example, a negative salary prevents employees_salary_ck from being added and enabled.
You can add a primary key in the same way:
ALTER TABLE employees
ADD CONSTRAINT employees_pk PRIMARY KEY (employee_id);The key column must contain no duplicate or NULL values. If the table already has invalid data, clean it before adding the constraint.
To add a NOT NULL rule, modify the column definition:
ALTER TABLE employees
MODIFY (email CONSTRAINT employees_email_nn NOT NULL);Existing rows must already contain a value in email.
Disabling and enabling constraints
You can temporarily disable a constraint during a controlled data load or maintenance operation.
ALTER TABLE employees
DISABLE CONSTRAINT employees_email_uk;
ALTER TABLE employees
ENABLE CONSTRAINT employees_email_uk;Disabling a constraint allows operations that would otherwise violate its rule. This creates a risk: invalid or duplicate data can enter the table while the constraint is disabled.
When you enable it again, Oracle normally checks the existing data. Use ENABLE NOVALIDATE when you want Oracle to enforce the rule for future changes but not check old rows:
ALTER TABLE employees
ENABLE NOVALIDATE CONSTRAINT employees_email_uk;Use this option only when you understand that old data may still violate the constraint.
The main states are:
| Command | Effect |
|---|---|
| `ENABLE` | Enforces the rule and validates existing rows |
| `DISABLE` | Stops enforcing the rule |
| `ENABLE NOVALIDATE` | Enforces new changes but does not validate old rows |
Dropping constraints
Drop a constraint by its name:
ALTER TABLE employees
DROP CONSTRAINT employees_salary_ck;Dropping permanently removes the rule. It does not remove the column or automatically repair data that was protected by the rule.
A primary key or unique constraint may be referenced by a foreign key in another table. Oracle normally prevents you from dropping the referenced key. Find and remove or change the dependent foreign key first. CASCADE can remove dependent foreign-key constraints, so use it only when that result is intended:
ALTER TABLE departments
DROP CONSTRAINT departments_pk CASCADE;Dropping a constraint is different from disabling it. Disabling is reversible with ENABLE; dropping requires you to add the constraint again if you later need it.
Exercise
How would you add a named check constraint requiring every employee status to be ACTIVE or INACTIVE, and later remove it?
Show answer
Run ALTER TABLE employees ADD CONSTRAINT employees_status_ck CHECK (status IN ('ACTIVE', 'INACTIVE'));, then run ALTER TABLE employees DROP CONSTRAINT employees_status_ck;.