0
0
SQLquery~20 mins

Foreign key ON DELETE behavior in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Foreign Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of ON DELETE CASCADE on child table rows

Consider two tables: authors and books. The books table has a foreign key author_id referencing authors(id) with ON DELETE CASCADE. If an author with id = 5 is deleted, what happens to the books written by that author?

SQL
CREATE TABLE authors (id INT PRIMARY KEY, name VARCHAR(100));
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 (5, 'Jane Doe');
INSERT INTO books VALUES (101, 'Book A', 5), (102, 'Book B', 5);

DELETE FROM authors WHERE id = 5;

SELECT * FROM books WHERE author_id = 5;
ABooks with author_id = 5 remain unchanged.
BNo rows in books with author_id = 5 remain; they are deleted.
CBooks with author_id = 5 have author_id set to NULL.
DDeletion of author with id 5 fails due to foreign key constraint.
Attempts:
2 left
💡 Hint

Think about what ON DELETE CASCADE means for dependent rows.

query_result
intermediate
2:00remaining
Behavior of ON DELETE SET NULL on foreign key

Given a orders table with a foreign key customer_id referencing customers(id) with ON DELETE SET NULL, what is the result of deleting a customer with id = 10?

SQL
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(100));
CREATE TABLE orders (id INT PRIMARY KEY, order_date DATE, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE SET NULL);

INSERT INTO customers VALUES (10, 'Alice');
INSERT INTO orders VALUES (201, '2024-01-01', 10), (202, '2024-01-02', 10);

DELETE FROM customers WHERE id = 10;

SELECT * FROM orders WHERE id IN (201, 202);
AThe orders with customer_id 10 remain, but their customer_id is set to NULL.
BThe orders with customer_id 10 are deleted.
CThe deletion of customer with id 10 fails due to foreign key constraint.
DThe orders with customer_id 10 remain unchanged.
Attempts:
2 left
💡 Hint

Consider what ON DELETE SET NULL does to foreign key columns.

🧠 Conceptual
advanced
2:00remaining
Understanding ON DELETE RESTRICT behavior

What happens if you try to delete a row from a parent table that has child rows referencing it, and the foreign key is defined with ON DELETE RESTRICT?

AThe parent row is deleted and child rows are deleted automatically.
BThe parent row is deleted but child rows remain with invalid foreign keys.
CThe parent row is deleted and child rows have their foreign key set to NULL.
DThe deletion of the parent row is blocked and raises an error.
Attempts:
2 left
💡 Hint

Think about what RESTRICT means in terms of deletion.

📝 Syntax
advanced
2:00remaining
Correct syntax for foreign key with ON DELETE SET DEFAULT

Which of the following SQL statements correctly creates a foreign key with ON DELETE SET DEFAULT behavior?

AALTER TABLE orders ADD FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE SET DEFAULT;
BALTER TABLE orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE SET NULL;
CALTER TABLE orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE SET DEFAULT;
DALTER TABLE orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE;
Attempts:
2 left
💡 Hint

Check the full syntax for adding a named foreign key constraint with ON DELETE behavior.

🔧 Debug
expert
2:00remaining
Diagnosing foreign key deletion error

You have two tables: departments and employees. The employees table has a foreign key department_id referencing departments(id) with ON DELETE NO ACTION. When you try to delete a department that has employees, you get an error. What is the cause?

SQL
CREATE TABLE departments (id INT PRIMARY KEY, name VARCHAR(100));
CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(100), department_id INT, FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE NO ACTION);

INSERT INTO departments VALUES (1, 'HR');
INSERT INTO employees VALUES (1001, 'John', 1);

DELETE FROM departments WHERE id = 1;
AThe foreign key constraint with ON DELETE NO ACTION prevents deleting a department if employees reference it.
BThe foreign key constraint is missing, so deletion fails.
CThe employees table has no rows, so deletion should succeed.
DThe ON DELETE NO ACTION allows deletion but sets department_id to NULL.
Attempts:
2 left
💡 Hint

Consider what ON DELETE NO ACTION means for deletion of referenced rows.