0
0
SQLquery~10 mins

Foreign key ON UPDATE behavior in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the action when the referenced key is updated.

SQL
CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES customers(id) ON UPDATE [1]
);
Drag options to blanks, or click blank then click option'
ACASCADE
BDELETE
CRESTRICT
DIGNORE
Attempts:
3 left
💡 Hint
Common Mistakes
Using DELETE instead of CASCADE causes errors because DELETE is not valid for ON UPDATE.
Using IGNORE is not a valid SQL action for ON UPDATE.
2fill in blank
medium

Complete the code to prevent updates to the referenced key if child rows exist.

SQL
CREATE TABLE enrollments (
  enrollment_id INT PRIMARY KEY,
  student_id INT,
  FOREIGN KEY (student_id) REFERENCES students(id) ON UPDATE [1]
);
Drag options to blanks, or click blank then click option'
ACASCADE
BRESTRICT
CSET NULL
DNO ACTION
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing RESTRICT with CASCADE, which allows updates to propagate.
Using SET NULL here would set child keys to NULL, not restrict updates.
3fill in blank
hard

Fix the error in the foreign key definition to correctly handle updates by setting child keys to NULL.

SQL
CREATE TABLE payments (
  payment_id INT PRIMARY KEY,
  order_id INT,
  FOREIGN KEY (order_id) REFERENCES orders(id) ON UPDATE [1]
);
Drag options to blanks, or click blank then click option'
ASET NULL
BNO DELETE
CRESTRICT
DCASCADE
Attempts:
3 left
💡 Hint
Common Mistakes
Using NO DELETE is invalid syntax and causes errors.
CASCADE updates child keys to new values instead of setting NULL.
4fill in blank
hard

Fill both blanks to create a foreign key that updates child keys on parent update and deletes child rows on parent delete.

SQL
CREATE TABLE comments (
  comment_id INT PRIMARY KEY,
  post_id INT,
  FOREIGN KEY (post_id) REFERENCES posts(id) ON UPDATE [1] ON DELETE [2]
);
Drag options to blanks, or click blank then click option'
ACASCADE
BSET NULL
CRESTRICT
DNO ACTION
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing CASCADE with RESTRICT can cause unexpected errors.
Using SET NULL on delete requires child keys to allow NULL.
5fill in blank
hard

Fill all three blanks to define a foreign key that restricts updates, sets child keys to NULL on delete, and uses NO ACTION on update.

SQL
CREATE TABLE subscriptions (
  subscription_id INT PRIMARY KEY,
  user_id INT,
  FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE [1] ON DELETE [2] ON UPDATE [3]
);
Drag options to blanks, or click blank then click option'
ARESTRICT
BSET NULL
CNO ACTION
DCASCADE
Attempts:
3 left
💡 Hint
Common Mistakes
Using CASCADE in all places can cause unwanted deletions or updates.
Confusing NO ACTION with RESTRICT; they behave differently in timing.