Bird
0
0

You have these tables:

hard📝 Application Q8 of 15
SQL - Table Relationships
You have these tables:
CREATE TABLE Teams (TeamID INT PRIMARY KEY);
CREATE TABLE Players (PlayerID INT PRIMARY KEY, TeamID INT, FOREIGN KEY (TeamID) REFERENCES Teams(TeamID) ON DELETE SET NULL ON UPDATE CASCADE);

If a team is deleted, what happens to the players belonging to that team? And what happens if the TeamID is updated in Teams?
APlayers are deleted on team delete; TeamID update fails
BPlayers' TeamID is set to NULL on delete; updated automatically on TeamID change
CPlayers remain unchanged on delete; TeamID update cascades
DDelete and update both fail due to foreign key constraints
Step-by-Step Solution
Solution:
  1. Step 1: Analyze ON DELETE SET NULL effect

    When a team is deleted, players' TeamID is set to NULL.
  2. Step 2: Analyze ON UPDATE CASCADE effect

    If TeamID changes, players' TeamID updates automatically to match.
  3. Final Answer:

    Players' TeamID is set to NULL on delete; updated automatically on TeamID change -> Option B
  4. Quick Check:

    ON DELETE SET NULL + ON UPDATE CASCADE behavior [OK]
Quick Trick: ON DELETE SET NULL nullifies; ON UPDATE CASCADE updates keys [OK]
Common Mistakes:
MISTAKES
  • Assuming players are deleted on team delete
  • Thinking updates fail with cascade
  • Ignoring nullability requirement for SET NULL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes