Bird
0
0

Given a table orders(order_id, customer_id, amount) with a foreign key on customer_id, what will happen if you try to insert an order with a customer_id that does not exist in the customers table?

medium📝 query result Q4 of 15
SQL - Database Design and Normalization
Given a table orders(order_id, customer_id, amount) with a foreign key on customer_id, what will happen if you try to insert an order with a customer_id that does not exist in the customers table?
AThe insert will succeed but customer_id will be set to NULL
BThe insert will succeed and create a new customer automatically
CThe insert will fail due to foreign key constraint violation
DThe insert will succeed and ignore the foreign key
Step-by-Step Solution
Solution:
  1. Step 1: Understand foreign key enforcement

    Foreign keys prevent inserting values that don't exist in the referenced table.
  2. Step 2: Predict insert behavior

    Inserting a non-existing customer_id violates the constraint, causing failure.
  3. Final Answer:

    The insert will fail due to foreign key constraint violation -> Option C
  4. Quick Check:

    Foreign key violation causes insert failure [OK]
Quick Trick: Foreign keys block inserts with invalid references [OK]
Common Mistakes:
  • Assuming automatic creation of referenced rows
  • Thinking foreign keys allow NULL silently
  • Believing foreign keys can be ignored on insert

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes