Bird
0
0

Which SQL statement correctly creates a foreign key for a one-to-many relationship from Orders to Customers?

easy📝 Syntax Q12 of 15
SQL - Table Relationships
Which SQL statement correctly creates a foreign key for a one-to-many relationship from Orders to Customers?
AALTER TABLE Customers ADD FOREIGN KEY (OrderID) REFERENCES Orders(OrderID);
BALTER TABLE Customers ADD FOREIGN KEY (CustomerID) REFERENCES Orders(OrderID);
CALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
DALTER TABLE Orders ADD FOREIGN KEY (OrderID) REFERENCES Customers(CustomerID);
Step-by-Step Solution
Solution:
  1. Step 1: Identify the 'many' and 'one' tables

    Orders is the 'many' side, Customers is the 'one' side in a one-to-many relationship.
  2. Step 2: Add foreign key in 'many' table referencing 'one' table

    The foreign key should be in Orders referencing Customers, so ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); is correct.
  3. Final Answer:

    ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); -> Option C
  4. Quick Check:

    Foreign key in 'many' table references 'one' table [OK]
Quick Trick: Foreign key goes in 'many' table pointing to 'one' table [OK]
Common Mistakes:
MISTAKES
  • Placing foreign key in the 'one' table instead of 'many'
  • Referencing wrong columns between tables
  • Mixing table names in foreign key definition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes