Bird
0
0

Consider these tables:

medium📝 query result Q5 of 15
SQL - Table Constraints
Consider these tables:
CREATE TABLE Orders (OrderID INT PRIMARY KEY, CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID));
CREATE TABLE Customers (CustomerID INT PRIMARY KEY, Name VARCHAR(50));
What will happen if you delete a customer who has orders, without specifying ON DELETE CASCADE?
AThe delete succeeds and sets CustomerID in orders to NULL.
BThe customer is deleted and orders remain with invalid CustomerID.
CThe customer is deleted and orders are automatically deleted.
DThe delete fails due to foreign key constraint.
Step-by-Step Solution
Solution:
  1. Step 1: Understand foreign key delete behavior

    Without ON DELETE CASCADE, deleting a parent referenced by child rows is blocked.
  2. Step 2: Check delete on customer with orders

    Since orders reference the customer, delete fails to prevent orphaned records.
  3. Final Answer:

    The delete fails due to foreign key constraint. -> Option D
  4. Quick Check:

    Delete parent with children = blocked without CASCADE [OK]
Quick Trick: Delete blocked if child rows exist without CASCADE [OK]
Common Mistakes:
MISTAKES
  • Assuming delete cascades automatically
  • Thinking child foreign keys become NULL automatically
  • Believing delete succeeds leaving invalid references

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes