Bird
0
0

You have this SQL statement:

medium📝 Debug Q6 of 15
SQL - Table Relationships
You have this SQL statement:
ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE SET NULL;

But when you try to add it, you get an error. What is a likely cause?
AThe Customers table does not have a primary key
BThe CustomerID column in Orders is NOT NULL
CThe syntax for ON DELETE SET NULL is incorrect
DForeign keys cannot be added with ALTER TABLE
Step-by-Step Solution
Solution:
  1. Step 1: Understand ON DELETE SET NULL requirement

    The foreign key column must allow NULL to set it to NULL on delete.
  2. Step 2: Check column nullability

    If CustomerID is NOT NULL, setting NULL on delete causes error.
  3. Final Answer:

    The CustomerID column in Orders is NOT NULL -> Option B
  4. Quick Check:

    ON DELETE SET NULL requires nullable foreign key column [OK]
Quick Trick: ON DELETE SET NULL needs nullable foreign key column [OK]
Common Mistakes:
MISTAKES
  • Assuming syntax is wrong
  • Thinking primary key absence causes this error
  • Believing foreign keys can't be added with ALTER TABLE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes