Sub topic 8
Foreign Keys and Referential Actions
Prerequisites: You should know what tables, rows, columns, primary keys, and basic CREATE TABLE statements are. You should also understand that a primary key identifies one row in a table.
Foreign Keys and Referential Integrity
A foreign key is a column, or group of columns, in one table that refers to a key in another table. The table containing the foreign key is the child table. The table containing the referenced key is the parent table.
For example, an employee belongs to a department. employee.dept_id can refer to department.dept_id.
Referential integrity is the rule that every non-null foreign-key value must match an existing value in the parent table. 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)
ON DELETE SET NULL
ON UPDATE CASCADE
);The primary key provides entity integrity: each department and employee has a unique, non-null identifier. The foreign key provides referential integrity. The CHECK and NOT NULL clauses provide domain restrictions.
A nullable foreign key allows an employee to have no department. In that case, dept_id is NULL, and the foreign-key match is not required. If every employee must belong to a department, declare the column NOT NULL.
Referential Actions
A referential action specifies what the database should do when a referenced parent key is updated or deleted.
| Action | Effect |
|---|---|
| `CASCADE` | Apply the parent change to matching child rows |
| `SET NULL` | Set matching foreign-key values to `NULL` |
| `RESTRICT` | Reject the parent change while child rows exist |
| `NO ACTION` | Usually reject the change when the constraint is checked |
For a delete, CASCADE deletes the child rows. SET NULL keeps the child rows but removes their parent reference. RESTRICT protects the parent row by refusing the delete.
For an update, ON UPDATE CASCADE changes child foreign-key values when the parent key changes. Primary keys are normally stable, so update actions are less common than delete actions.
Choosing an Action
Choose based on the meaning of the relationship:
- Use
CASCADEfor dependent data, such as order items owned by an order. - Use
SET NULLwhen the child should remain after its parent is removed. - Use
RESTRICTwhen deleting the parent would violate a business rule. - Make the foreign key
NOT NULLwhen the relationship is mandatory.
The database checks these rules during INSERT, UPDATE, and DELETE operations. An insert into employee with dept_id = 7 fails if department 7 does not exist.
Exercise
An employee row has dept_id = 4. Department 4 is deleted, and the foreign key uses ON DELETE SET NULL. What happens?
Show answer
The employee row remains, but its deptid becomes NULL. This is valid only if deptid permits NULL.