Consider two tables: Authors and Books.
The Books table has a foreign key author_id referencing Authors(id) with ON DELETE CASCADE.
If you delete the author with id = 3, which rows in Books will be deleted?
CREATE TABLE Authors (id INT PRIMARY KEY, name VARCHAR(50)); CREATE TABLE Books (id INT PRIMARY KEY, title VARCHAR(100), author_id INT, FOREIGN KEY (author_id) REFERENCES Authors(id) ON DELETE CASCADE); INSERT INTO Authors VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Carol'); INSERT INTO Books VALUES (10, 'Book A', 1), (11, 'Book B', 3), (12, 'Book C', 3), (13, 'Book D', 2);
Think about which books belong to the author being deleted.
Deleting author with id 3 triggers cascade delete on books with author_id = 3, which are books 11 and 12.
What is the main effect of ON DELETE CASCADE on a foreign key constraint?
Think about what happens to dependent rows when the main row is removed.
ON DELETE CASCADE means deleting a parent row causes automatic deletion of all child rows referencing it.
Which SQL statement correctly creates a foreign key with ON DELETE CASCADE?
Look for the exact phrase used to specify cascade delete.
The correct syntax uses ON DELETE CASCADE after the foreign key reference.
Given these tables, deleting a parent row does not delete child rows. Why?
CREATE TABLE Parent (id INT PRIMARY KEY); CREATE TABLE Child (id INT PRIMARY KEY, parent_id INT, FOREIGN KEY (parent_id) REFERENCES Parent(id));
Delete statement: DELETE FROM Parent WHERE id = 1;
Check the foreign key definition for cascade behavior.
Without ON DELETE CASCADE, deleting a parent row does not affect child rows.
You have a large parent table and a child table with millions of rows. Deleting a parent row cascades deletes to many child rows, but the operation is slow. Which approach can improve performance?
Think about how the database finds child rows to delete.
Indexing the foreign key column helps the database quickly locate child rows to delete, improving cascade delete speed.