Bird
0
0

You want to enforce referential integrity between Orders and Customers tables. You also want to prevent deleting a customer if they have orders. Which foreign key option should you use?

hard📝 Application Q9 of 15
SQL - Table Relationships
You want to enforce referential integrity between Orders and Customers tables. You also want to prevent deleting a customer if they have orders. Which foreign key option should you use?
AFOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE RESTRICT
BFOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE CASCADE
CFOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE SET NULL
DFOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE NO ACTION
Step-by-Step Solution
Solution:
  1. Step 1: Understand delete options

    ON DELETE RESTRICT prevents deleting parent if child rows exist.
  2. Step 2: Match requirement

    To prevent deleting customers with orders, use ON DELETE RESTRICT.
  3. Final Answer:

    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE RESTRICT -> Option A
  4. Quick Check:

    ON DELETE RESTRICT blocks parent delete if children exist [OK]
Quick Trick: Use ON DELETE RESTRICT to block parent delete with children [OK]
Common Mistakes:
MISTAKES
  • Using CASCADE which deletes children
  • Using SET NULL which nullifies foreign keys
  • Confusing NO ACTION with RESTRICT behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes