Sub topic 3
How Constraints Work: Inline, Out-of-Line, and Named
This chapter assumes that you know basic CREATE TABLE, INSERT, and SELECT statements. You should also understand that a table stores rows and columns, and that a key identifies or connects rows.
A constraint is a rule that Oracle checks when data changes. Constraints protect the table from invalid data. Oracle supports these common constraint types:
| Constraint | Rule it enforces |
|---|---|
| `NOT NULL` | A column must contain a value |
| `PRIMARY KEY` | Each row has a unique, non-null identifier |
| `UNIQUE` | Values, or combinations of values, cannot repeat |
| `CHECK` | Values must satisfy a condition |
| `FOREIGN KEY` | Values must refer to an existing row in another table |
Inline and out-of-line constraints
An inline constraint appears in the definition of the column that it restricts. It is useful when the rule concerns one column.
An out-of-line constraint appears after the column definitions, separated by commas. It is useful for multi-column rules and makes a table's rules easier to scan in one place.
This example uses both styles:
CREATE TABLE departments (
department_id NUMBER,
department_name VARCHAR2(100)
CONSTRAINT departments_name_nn NOT NULL,
CONSTRAINT departments_pk
PRIMARY KEY (department_id),
CONSTRAINT departments_name_uk
UNIQUE (department_name)
);
CREATE TABLE employees (
employee_id NUMBER,
email VARCHAR2(255)
CONSTRAINT employees_email_nn NOT NULL,
salary NUMBER(10,2),
status VARCHAR2(10) DEFAULT 'ACTIVE'
CONSTRAINT employees_status_nn NOT NULL,
department_id NUMBER,
CONSTRAINT employees_pk
PRIMARY KEY (employee_id),
CONSTRAINT employees_email_uk
UNIQUE (email),
CONSTRAINT employees_salary_ck
CHECK (salary >= 0),
CONSTRAINT employees_status_ck
CHECK (status IN ('ACTIVE', 'INACTIVE')),
CONSTRAINT employees_dept_fk
FOREIGN KEY (department_id)
REFERENCES departments (department_id)
);The NOT NULL rules are inline because they belong directly to individual columns. The other rules are out-of-line. The foreign key refers to a column in another table, while the primary key, unique, and check constraints are defined after all columns.
A primary key also implies NOT NULL, so employee_id cannot be omitted. A foreign key can contain NULL unless its column also has a NOT NULL constraint.
Why name constraints
Oracle can generate names for unnamed constraints. Generated names are often difficult to recognize, such as SYS_C008123. Explicit names make errors easier to understand and make later maintenance possible.
For example, an invalid insert can identify the rule that failed:
INSERT INTO employees (employee_id, email, salary, department_id)
VALUES (1, '[email protected]', -25, 10);This statement can fail because employees_salary_ck rejects the negative salary, or because employees_dept_fk cannot find department 10. The named constraint tells you which data rule needs attention.
Constraint names must be unique within the schema. A consistent prefix helps identify the table and rule type:
_pkfor primary keys_ukfor unique constraints_ckfor check constraints_fkfor foreign keys_nnfor not-null constraints
Single-column and multi-column rules
Inline syntax is convenient for a single column. Use an out-of-line constraint for a rule involving several columns:
CREATE TABLE project_assignments (
employee_id NUMBER NOT NULL,
project_id NUMBER NOT NULL,
role_name VARCHAR2(50),
CONSTRAINT project_assignments_pk
PRIMARY KEY (employee_id, project_id),
CONSTRAINT project_assignments_role_ck
CHECK (role_name IS NULL OR LENGTH(role_name) >= 2)
);Here, neither employee_id nor project_id is unique by itself. Their combination identifies one assignment.
Exercise
Which constraint style should you use for a primary key composed of employeeid and projectid?
Show answer
Use an out-of-line named constraint, such as CONSTRAINT assignmentspk PRIMARY KEY (employeeid, project_id).