SQLMentor // glossary

Constraint

A constraint is a rule enforced by the database on a column or table, rejecting any data that violates it. Common types are PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, and CHECK.

Constraints move data-integrity rules out of application code and into the database, where they apply no matter which program writes the data. A CHECK constraint enforces a condition (e.g. salary > 0); a UNIQUE constraint forbids duplicate values.

Because the database enforces them on every insert and update, constraints are the most reliable place to guarantee data quality.

Example

CREATE TABLE employees (
  employee_id NUMBER PRIMARY KEY,
  email       VARCHAR2(100) UNIQUE,
  salary      NUMBER CHECK (salary > 0)
);