0
0
SQLquery~10 mins

Composite primary keys 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 define a composite primary key on columns 'order_id' and 'product_id'.

SQL
CREATE TABLE order_details (order_id INT, product_id INT, quantity INT, PRIMARY KEY ([1]));
Drag options to blanks, or click blank then click option'
Aorder_id, product_id
Bproduct_id
Corder_id
Dquantity
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one column as primary key instead of both.
Including columns that are not part of the key.
2fill in blank
medium

Complete the code to add a composite primary key constraint named 'pk_enrollment' on 'student_id' and 'course_id'.

SQL
ALTER TABLE enrollments ADD CONSTRAINT pk_enrollment [1] (student_id, course_id);
Drag options to blanks, or click blank then click option'
ACHECK
BPRIMARY KEY
CUNIQUE
DFOREIGN KEY
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'FOREIGN KEY' instead of 'PRIMARY KEY'.
Using 'UNIQUE' which allows NULLs.
3fill in blank
hard

Fix the error in the composite primary key definition by completing the code.

SQL
CREATE TABLE attendance (student_id INT, class_id INT, date DATE, PRIMARY KEY [1]);
Drag options to blanks, or click blank then click option'
A{student_id, class_id}
B[student_id, class_id]
Cstudent_id, class_id
D(student_id, class_id)
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses causes syntax errors.
Using square brackets or curly braces instead of parentheses.
4fill in blank
hard

Fill both blanks to create a table 'subscriptions' with a composite primary key on 'user_id' and 'service_id'.

SQL
CREATE TABLE subscriptions (user_id INT, service_id INT, start_date DATE, [1] PRIMARY KEY [2]);
Drag options to blanks, or click blank then click option'
ACONSTRAINT sub_pk
BFOREIGN KEY
C(user_id, service_id)
DUNIQUE
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'FOREIGN KEY' instead of 'PRIMARY KEY'.
Not enclosing columns in parentheses.
5fill in blank
hard

Fill all three blanks to define a composite primary key named 'pk_order_item' on 'order_id' and 'item_id' in the 'order_items' table.

SQL
CREATE TABLE order_items (order_id INT, item_id INT, quantity INT, [1] pk_order_item [2] [3] (order_id, item_id));
Drag options to blanks, or click blank then click option'
ACONSTRAINT
BPRIMARY
CKEY
DFOREIGN
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'FOREIGN' instead of 'PRIMARY'.
Omitting 'CONSTRAINT' keyword.