0
0
SQLquery~10 mins

FOREIGN KEY constraint 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 create a foreign key constraint linking the orders table to the customers table.

SQL
CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES [1](customer_id)
);
Drag options to blanks, or click blank then click option'
Aemployees
Bproducts
Corders
Dcustomers
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing the wrong table name in the foreign key constraint.
Using the same table name as the current table.
2fill in blank
medium

Complete the code to add a foreign key constraint to the payments table referencing the orders table.

SQL
ALTER TABLE payments
ADD CONSTRAINT fk_order FOREIGN KEY (order_id) REFERENCES [1](order_id);
Drag options to blanks, or click blank then click option'
Aorders
Bcustomers
Cpayments
Dinvoices
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing the wrong table in the foreign key.
Trying to reference the same table being altered.
3fill in blank
hard

Fix the error in the foreign key constraint definition by completing the code.

SQL
CREATE TABLE order_items (
  item_id INT PRIMARY KEY,
  order_id INT,
  CONSTRAINT fk_order FOREIGN KEY (order_id) REFERENCES [1](order_id)
);
Drag options to blanks, or click blank then click option'
Aorders
Bcustomers
Citems
Dorder_items
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing the same table in the foreign key constraint.
Referencing an unrelated table.
4fill in blank
hard

Fill both blanks to create a foreign key constraint with cascading delete from employees to departments.

SQL
CREATE TABLE employees (
  emp_id INT PRIMARY KEY,
  dept_id INT,
  CONSTRAINT fk_dept FOREIGN KEY ([1]) REFERENCES departments([2]) ON DELETE CASCADE
);
Drag options to blanks, or click blank then click option'
Adept_id
Bemp_id
Cdepartment_id
Demployee_id
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping foreign key and referenced key columns.
Using incorrect column names.
5fill in blank
hard

Fill all three blanks to create a foreign key constraint on enrollments referencing students with cascading update and delete.

SQL
CREATE TABLE enrollments (
  enrollment_id INT PRIMARY KEY,
  student_id INT,
  CONSTRAINT fk_student FOREIGN KEY ([1]) REFERENCES [2]([3]) ON UPDATE CASCADE ON DELETE CASCADE
);
Drag options to blanks, or click blank then click option'
Astudent_id
Bstudents
Denrollments
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing the wrong table or column.
Forgetting to match foreign key and referenced column names.