0
0
SQLquery~20 mins

Referential integrity enforcement in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Referential Integrity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What happens when deleting a parent row with ON DELETE CASCADE?

Given two tables departments and employees where employees.department_id references departments.id with ON DELETE CASCADE, what will be the result of deleting a department?

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

INSERT INTO departments VALUES (1, 'HR'), (2, 'IT');
INSERT INTO employees VALUES (1, 'Alice', 1), (2, 'Bob', 2), (3, 'Charlie', 1);

DELETE FROM departments WHERE id = 1;

SELECT * FROM employees ORDER BY id;
AOnly the department row is deleted; employees remain
BDelete fails with foreign key constraint error
CRows with department_id = 1 are deleted; remaining employees: (2, 'Bob', 2)
DAll employees remain unchanged
Attempts:
2 left
💡 Hint

Think about what ON DELETE CASCADE means for child rows.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a foreign key with ON UPDATE SET NULL?

Choose the correct SQL statement to create a foreign key constraint that sets the child column to NULL when the parent key is updated.

AFOREIGN KEY (child_id) REFERENCES parent(id) ON UPDATE NO ACTION
BFOREIGN KEY (child_id) REFERENCES parent(id) ON UPDATE CASCADE
CFOREIGN KEY (child_id) REFERENCES parent(id) ON DELETE SET NULL
DFOREIGN KEY (child_id) REFERENCES parent(id) ON UPDATE SET NULL
Attempts:
2 left
💡 Hint

Focus on the ON UPDATE clause and the action SET NULL.

🔧 Debug
advanced
2:00remaining
Why does this foreign key constraint fail to create?

Given the following SQL, why does the foreign key constraint creation fail?

SQL
CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE RESTRICT
);

CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);
APrimary key must be declared on <code>customer_id</code> in <code>orders</code>
BThe referenced table <code>customers</code> must be created before <code>orders</code>
CThe <code>ON DELETE RESTRICT</code> clause is invalid syntax
DThe foreign key column <code>customer_id</code> must be declared NOT NULL
Attempts:
2 left
💡 Hint

Think about the order of table creation and foreign key references.

optimization
advanced
2:00remaining
How to optimize foreign key checks for bulk inserts?

You need to insert thousands of rows into a child table with foreign keys. Which approach optimizes performance while maintaining referential integrity?

ADisable foreign key checks before insert, then re-enable after all inserts
BInsert rows one by one with foreign key checks enabled
CDrop foreign key constraints permanently before insert
DInsert rows in any order without foreign key constraints
Attempts:
2 left
💡 Hint

Consider temporarily disabling checks to speed up bulk operations.

🧠 Conceptual
expert
2:00remaining
What is the effect of ON DELETE SET DEFAULT in referential integrity?

Consider a foreign key with ON DELETE SET DEFAULT. What happens when the referenced parent row is deleted?

AThe child foreign key column is set to its default value
BThe child row is deleted automatically
CThe delete operation is rejected with an error
DThe child foreign key column is set to NULL
Attempts:
2 left
💡 Hint

Think about what SET DEFAULT means for a foreign key column.