Tables, Keys & Constraints · Question 17 · Hard

Tables, Keys & Constraints question 17

Emp.dept_id is declared as FOREIGN KEY REFERENCES Dept(id) with no ON DELETE clause, and foreign keys are being enforced. Three employees have dept_id = 5. What happens when DELETE FROM Dept WHERE id = 5 runs?

  1. The department and its three employees are deleted
  2. The department is deleted, and dept_id becomes NULL for the three employees
  3. The department is deleted, and the three employees keep dept_id = 5
  4. The DELETE is rejected with an error, and nothing changes
Answer and explanation

Answer: D. The DELETE is rejected with an error, and nothing changes

Explanation: Without an ON DELETE clause the default action is NO ACTION, which behaves like RESTRICT here: a parent row cannot be deleted while child rows still refer to it. Cascading or setting NULL happens only when it is asked for explicitly.

Previous: The second INSERT passes NULL as the price. What does the final SELECT return? · Next: The status column has a DEFAULT, but this INSERT passes NULL for it…

Practise tables, keys & constraints

Try a fresh set of tables, keys & constraints questions with an explanation after every answer, free and without signing up: Tables, Keys & Constraints practice. Or sit a full placement mock test.

More tables, keys & constraints questions

More sql practice

DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY · Subqueries & CTEs · Window Functions · NULL Handling · SQL Functions & CASE

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge