Bird
0
0

Given these tables:

medium📝 query result Q13 of 15
SQL - Table Relationships
Given these tables:
CREATE TABLE Customers (CustomerID INT PRIMARY KEY, Name VARCHAR(50));
CREATE TABLE Orders (OrderID INT PRIMARY KEY, CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE CASCADE);

What happens if a customer with CustomerID = 5 is deleted?
AAll orders with CustomerID = 5 are also deleted
BThe delete fails due to foreign key constraint
COrders with CustomerID = 5 remain unchanged
DCustomerID in Orders is set to NULL for those orders
Step-by-Step Solution
Solution:
  1. Step 1: Understand ON DELETE CASCADE

    The ON DELETE CASCADE option means deleting a referenced row causes all related rows to be deleted automatically.
  2. Step 2: Apply to the scenario

    Deleting customer with CustomerID=5 will delete all orders linked to that customer in Orders table.
  3. Final Answer:

    All orders with CustomerID = 5 are also deleted -> Option A
  4. Quick Check:

    ON DELETE CASCADE = delete related rows [OK]
Quick Trick: ON DELETE CASCADE deletes related rows automatically [OK]
Common Mistakes:
MISTAKES
  • Thinking delete will fail due to constraint
  • Assuming related rows remain unchanged
  • Confusing CASCADE with SET NULL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes