Sub topic 7
Integrity Constraints: Domain, Entity, and Referential
This chapter assumes you know basic set notation, tables, rows, columns, and introductory SQL. An integrity constraint is a rule that prevents invalid data from entering a database. The main constraints here protect domains, entities, and references.
Domain Integrity
A domain is the set of allowed values for an attribute. Domain integrity requires every value to have the correct type and satisfy any stated restrictions.
For example, an employee salary might need to be a positive number. A department name might be required and unique. SQL expresses these rules with data types and constraints such as NOT NULL, CHECK, and UNIQUE.
- NOT NULL requires a value.
- CHECK tests a condition.
- UNIQUE prevents duplicate non-null values in a column or column combination.
- A data type restricts the general form of a value.
Entity Integrity
Entity integrity says that every row must be identifiable. A primary key enforces this rule.
A primary key has two properties:
- It is unique for every row.
- It cannot be NULL.
A table can have only one primary key, although that key can contain several columns. A key containing several columns is a composite key.
A candidate key is any minimal set of attributes that uniquely identifies rows. The database designer selects one candidate key as the primary key. Other candidate keys can be declared UNIQUE and are often called alternate keys.
Referential Integrity
Referential integrity keeps relationships between tables valid. A foreign key is an attribute, or group of attributes, in one table that refers to a candidate key, usually the primary key, in another table.
Every non-null foreign-key value must match a referenced value. Alternatively, the foreign key may be NULL when the relationship is optional. This prevents an employee from referring to a department that does not exist.
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
salary NUMERIC(10,2) CHECK (salary > 0),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);| Constraint | Protects | Example |
|---|---|---|
| Domain | Valid attribute values | salary must be greater than zero |
| Entity | Row identity | emp_id is unique and not NULL |
| Referential | Valid table relationships | dept_id must reference department |
How the Rules Work Together
When inserting an employee, the database checks the data type, CHECK conditions, required values, primary-key uniqueness, and foreign-key reference. When updating or deleting a referenced department, additional referential actions determine what happens to its employees.
Exercise
Which constraints are violated if an employee row has empid = 7, salary = -20, and deptid = 99 when no department 99 exists?
Show answer
The CHECK constraint on salary and the foreign-key constraint on deptid are violated. The primary-key constraint is not violated unless another row already has empid = 7.