Skip to main content

What does ON DELETE CASCADE do?

ON DELETE CASCADE is a rule for a foreign key that tells the database:

If a record in the parent table is deleted, automatically delete every related record in the child table.

Example:

Tables:

  • Departments (ID, Name)
  • Employees (ID, Name, DepartmentID)

Creating a foreign key with a cascade:

sql
ALTER TABLE Employees ADD CONSTRAINT fk_department FOREIGN KEY (DepartmentID) REFERENCES Departments(ID) ON DELETE CASCADE;

What happens

  • If a department is deleted from the Departments table, every employee belonging to that department also gets automatically deleted from the Employees table.

Why this is needed:

  • To avoid leaving "orphaned references" to records that no longer exist.
  • It simplifies managing related data, especially during deletes.

Put simply, ON DELETE CASCADE automatically removes every dependent record, keeping the database consistent.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.