ON DELETE CASCADE
ON DELETE CASCADE is a foreign key option that automatically deletes child rows when the parent row they reference is deleted, keeping referential integrity without orphaned records.
Without a cascade rule, deleting a department that still has employees referencing it would either fail (blocked by the foreign key) or, worse, leave orphaned employee rows pointing at a department that no longer exists. ON DELETE CASCADE deletes those employee rows automatically along with the department.
The alternatives are ON DELETE SET NULL (clear the foreign key instead of deleting the row) and ON DELETE RESTRICT (the default in most databases — block the delete entirely while children exist). Cascading deletes are powerful and dangerous — use them only where "the child truly can't exist without the parent" is really true.
ALTER TABLE employees
ADD CONSTRAINT fk_dept
FOREIGN KEY (department_id)
REFERENCES departments(department_id)
ON DELETE CASCADE;