Bird
0
0

Which SQL command correctly creates a foreign key constraint to enforce referential integrity between Orders and Customers tables?

easy📝 Syntax Q3 of 15
SQL - Table Relationships
Which SQL command correctly creates a foreign key constraint to enforce referential integrity between Orders and Customers tables?
AALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
BCREATE TABLE Orders (OrderID INT, CustomerID INT, FOREIGN KEY CustomerID REFERENCES Customers);
CALTER TABLE Customers ADD FOREIGN KEY (CustomerID) REFERENCES Orders(OrderID);
DCREATE FOREIGN KEY Orders(CustomerID) REFERENCES Customers(CustomerID);
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct syntax for adding a foreign key

    The correct syntax uses ALTER TABLE with ADD CONSTRAINT and specifies the foreign key column and referenced table and column.
  2. Step 2: Verify the referenced table and column

    Orders.CustomerID references Customers.CustomerID, which matches the foreign key relationship.
  3. Final Answer:

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

    Check syntax correctness and referenced columns [OK]
Quick Trick: Use ALTER TABLE ADD CONSTRAINT for foreign keys [OK]
Common Mistakes:
MISTAKES
  • Incorrect syntax for foreign key creation
  • Referencing wrong table or column
  • Missing CONSTRAINT keyword
  • Using CREATE FOREIGN KEY instead of ALTER TABLE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes