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?
- The department and its three employees are deleted
- The department is deleted, and dept_id becomes NULL for the three employees
- The department is deleted, and the three employees keep dept_id = 5
- 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
- The primary key below is made of two columns. What does the final SELECT return?
- A table Orders with 50 lakh rows is searched very often by customer_id, so an…
- The second INSERT passes NULL as the price. What does the final SELECT return?
- The status column has a DEFAULT, but this INSERT passes NULL for it…
- The four INSERT statements run one after another. Which of them is the first…
- In standard SQL, which column(s) of the parent table may a FOREIGN KEY…
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