Bird
0
0

Which of these statements has an error?

medium📝 Debug Q14 of 15
SQL - Database Design and Normalization
You have a table orders with columns order_id (primary key), customer_id, and order_date. You want to add a foreign key constraint on customer_id referencing customers(customer_id). Which of these statements has an error?
AALTER TABLE orders ADD FOREIGN KEY (customer_id) REFERENCES customers(customer_id);
BALTER TABLE orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(customer_id);
CALTER TABLE orders ADD FOREIGN KEY customer_id REFERENCES customers(customer_id);
DALTER TABLE orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE;
Step-by-Step Solution
Solution:
  1. Step 1: Review correct syntax for adding foreign keys

    The correct syntax requires parentheses around the column name after FOREIGN KEY.
  2. Step 2: Identify the incorrect statement

    ALTER TABLE orders ADD FOREIGN KEY customer_id REFERENCES customers(customer_id); misses parentheses around customer_id, causing syntax error.
  3. Final Answer:

    ALTER TABLE orders ADD FOREIGN KEY customer_id REFERENCES customers(customer_id); -> Option C
  4. Quick Check:

    Foreign key column must be in parentheses [OK]
Quick Trick: Always use parentheses around foreign key columns [OK]
Common Mistakes:
  • Omitting parentheses around column names
  • Forgetting to name the constraint (optional but recommended)
  • Misplacing ON DELETE options

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes