0
0
SQLquery~10 mins

Foreign key ON DELETE 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 that deleting a parent row will delete matching child rows.

SQL
CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE [1]
);
Drag options to blanks, or click blank then click option'
ACASCADE
BRESTRICT
CSET NULL
DNO ACTION
Attempts:
3 left
💡 Hint
Common Mistakes
Using RESTRICT which prevents deletion if child rows exist.
Using SET NULL without allowing NULL in the foreign key column.
2fill in blank
medium

Complete the code to prevent deletion of a parent row if child rows exist.

SQL
ALTER TABLE orders
ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE [1];
Drag options to blanks, or click blank then click option'
ARESTRICT
BCASCADE
CSET NULL
DNO ACTION
Attempts:
3 left
💡 Hint
Common Mistakes
Using CASCADE which deletes child rows automatically.
Confusing NO ACTION with RESTRICT (they behave similarly but differ in timing).
3fill in blank
hard

Fix the error in the foreign key definition to set the child foreign key to NULL when the parent is deleted.

SQL
CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE [1]
);
Drag options to blanks, or click blank then click option'
ACASCADE
BRESTRICT
CSET NULL
DNO ACTION
Attempts:
3 left
💡 Hint
Common Mistakes
Using CASCADE which deletes child rows instead of setting NULL.
Using RESTRICT which prevents deletion of the parent row.
4fill in blank
hard

Fill both blanks to create a foreign key that prevents deletion and updates child keys on parent update.

SQL
CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE [1] ON UPDATE [2]
);
Drag options to blanks, or click blank then click option'
ARESTRICT
BCASCADE
CSET NULL
DNO ACTION
Attempts:
3 left
💡 Hint
Common Mistakes
Using CASCADE for ON DELETE which deletes child rows.
Using NO ACTION which may defer enforcement.
5fill in blank
hard

Fill all three blanks to create a foreign key that sets child keys to NULL on delete, cascades updates, and restricts deletes on another key.

SQL
CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  product_id INT,
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE [1] ON UPDATE [2],
  FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE [3]
);
Drag options to blanks, or click blank then click option'
ASET NULL
BCASCADE
CRESTRICT
DNO ACTION
Attempts:
3 left
💡 Hint
Common Mistakes
Using CASCADE for delete on product_id which deletes child rows.
Using NO ACTION which may not enforce immediate constraints.