Bird
0
0

You wrote this SQL to create a one-to-many relationship:

medium📝 Debug Q14 of 15
SQL - Table Relationships
You wrote this SQL to create a one-to-many relationship:
CREATE TABLE Orders (OrderID INT PRIMARY KEY, CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID));

But you get an error. What is the most likely cause?
AOrderID should not be primary key in Orders
BCustomers table does not exist or CustomerID is not a primary key
CForeign key should be in Customers table, not Orders
DCustomerID column type must be VARCHAR, not INT
Step-by-Step Solution
Solution:
  1. Step 1: Check foreign key reference validity

    Foreign key must reference an existing table and a primary or unique key column.
  2. Step 2: Verify Customers table and CustomerID key

    If Customers table or CustomerID primary key is missing, error occurs.
  3. Final Answer:

    Customers table does not exist or CustomerID is not a primary key -> Option B
  4. Quick Check:

    Foreign key references must exist and be keys [OK]
Quick Trick: Foreign key target must exist and be primary/unique key [OK]
Common Mistakes:
MISTAKES
  • Assuming foreign key can reference non-key columns
  • Placing foreign key in wrong table
  • Mismatching data types between foreign key and referenced key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes