CASCADE delete preview in SQL - Time & Space Complexity
When we delete a record that has related records in other tables, the database may delete those related records automatically using CASCADE delete.
We want to understand how the time to delete grows as the number of related records increases.
Analyze the time complexity of this SQL snippet using CASCADE delete.
-- Assume tables: Orders and OrderItems
-- OrderItems has a foreign key to Orders with ON DELETE CASCADE
DELETE FROM Orders
WHERE OrderID = 123;
This deletes one order and automatically deletes all its related order items.
Look for repeated actions caused by the CASCADE delete.
- Primary operation: Deleting each related record in the child table (OrderItems).
- How many times: Once for each related order item linked to the deleted order.
The time to delete grows with the number of related records to remove.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 related items | Deleting 1 order + 10 order items |
| 100 related items | Deleting 1 order + 100 order items |
| 1000 related items | Deleting 1 order + 1000 order items |
Pattern observation: The total work increases roughly in direct proportion to the number of related records.
Time Complexity: O(n)
This means the time to complete the delete grows linearly with the number of related records that must be deleted.
[X] Wrong: "Deleting one record is always a quick, constant-time operation regardless of related data."
[OK] Correct: Because CASCADE delete removes all related records, the time depends on how many related records exist, not just the one main record.
Understanding how CASCADE delete affects performance shows you can think about real database behaviors beyond simple queries.
This skill helps you explain and predict how data changes impact system speed in practical situations.
"What if the foreign key was set to SET NULL instead of CASCADE? How would the time complexity change when deleting a record?"
Practice
CASCADE DELETE do in a database?Solution
Step 1: Understand CASCADE DELETE behavior
CASCADE DELETE means when you delete a parent row, all related child rows linked by foreign keys are also deleted automatically.Step 2: Compare options with this behavior
Automatically deletes related child rows when a parent row is deleted matches this behavior exactly. Options A, B, and D describe different or incorrect behaviors.Final Answer:
Automatically deletes related child rows when a parent row is deleted -> Option DQuick Check:
CASCADE DELETE = auto-remove child rows [OK]
- Thinking CASCADE prevents deletion
- Confusing CASCADE with backup
- Assuming child rows stay after parent deletion
orders table with customer_id = 5?Solution
Step 1: Identify child rows linked to parent rows
To preview cascade deletes, select child rows where foreign key matches parent rows to be deleted. Here, order_items link to orders by order_id.Step 2: Use subquery to find parent rows with customer_id = 5
The subquery selects orders with customer_id = 5. The outer query selects order_items with those order_ids.Final Answer:
SELECT * FROM order_items WHERE order_id IN (SELECT id FROM orders WHERE customer_id = 5); -> Option CQuick Check:
Preview child rows with IN + subquery [OK]
- Trying to delete instead of preview
- Selecting wrong table without join
- Using wrong column in WHERE clause
departments and employees with employees.department_id referencing departments.id with CASCADE DELETE, what rows will this query return?SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');
Solution
Step 1: Understand the subquery
The subquery selects department ids where the name is 'Sales'.Step 2: Outer query filters employees by those department ids
Employees with department_id matching those ids are returned.Final Answer:
All employees who work in the Sales department -> Option AQuick Check:
IN subquery filters child rows correctly [OK]
- Confusing employees with departments
- Assuming syntax error
- Ignoring the subquery effect
SELECT * FROM order_items WHERE order_id = (SELECT id FROM orders WHERE customer_id = 10);
Why might this query cause an error?
Solution
Step 1: Analyze subquery result
The subquery selects all order ids for customer_id 10, which can be multiple rows.Step 2: Check operator usage
The outer query uses '=' which expects a single value, causing an error if multiple rows returned.Final Answer:
Subquery returns multiple rows but '=' expects one value -> Option AQuick Check:
Use IN for multiple subquery results [OK]
- Using '=' with multi-row subquery
- Assuming table or column names are wrong
- Ignoring subquery result count
country = 'USA'. The schema has customers and orders (orders.customer_id references customers.id with CASCADE). Which query correctly previews all orders to be deleted?Solution
Step 1: Identify parent rows to delete
Customers with country = 'USA' are the parent rows to be deleted.Step 2: Find child rows linked to those parents
Orders with customer_id matching those customers will be deleted by CASCADE.Step 3: Write query to preview child rows
Use IN with subquery selecting customer ids from customers where country = 'USA'.Final Answer:
SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE country = 'USA'); -> Option BQuick Check:
Preview child rows with IN + subquery [OK]
- Selecting from wrong table
- Using DELETE instead of SELECT
- Filtering child table by parent's column directly
Ready to prove it?
Pass the test and this pattern moves unknown
