0
0
SQLquery~10 mins

Primary keys and uniqueness 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 primary key on the 'id' column.

SQL
CREATE TABLE users (id INT [1], name VARCHAR(100));
Drag options to blanks, or click blank then click option'
APRIMARY KEY
BUNIQUE
CNOT NULL
DFOREIGN KEY
Attempts:
3 left
💡 Hint
Common Mistakes
Using UNIQUE instead of PRIMARY KEY does not define the primary key.
Using FOREIGN KEY is incorrect here as it references another table.
2fill in blank
medium

Complete the code to add a unique constraint on the 'email' column.

SQL
ALTER TABLE users ADD CONSTRAINT unique_email [1] (email);
Drag options to blanks, or click blank then click option'
APRIMARY KEY
BCHECK
CFOREIGN KEY
DUNIQUE
Attempts:
3 left
💡 Hint
Common Mistakes
Using PRIMARY KEY here would be wrong because the table already has a primary key.
CHECK constraints are for conditions, not uniqueness.
3fill in blank
hard

Fix the error in the code to correctly define a composite primary key on 'order_id' and 'product_id'.

SQL
CREATE TABLE order_items (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
Border_id
Cproduct_id
Dquantity
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one column instead of both for the composite key.
Including a non-key column like 'quantity' in the primary key.
4fill in blank
hard

Fill both blanks to create a table with a unique constraint on 'username' and a primary key on 'user_id'.

SQL
CREATE TABLE accounts (user_id INT [1], username VARCHAR(50) [2]);
Drag options to blanks, or click blank then click option'
APRIMARY KEY
BNOT NULL
CUNIQUE
DDEFAULT
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing UNIQUE with PRIMARY KEY.
Not adding NOT NULL to primary key column (though PRIMARY KEY implies NOT NULL).
5fill in blank
hard

Fill all three blanks to add a primary key and unique constraint using table-level constraints.

SQL
CREATE TABLE products (product_id INT, name VARCHAR(100), sku VARCHAR(50), CONSTRAINT [1] PRIMARY KEY ([2]), CONSTRAINT [3] UNIQUE (sku));
Drag options to blanks, or click blank then click option'
Apk_products
Bproduct_id
Cuq_sku
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up constraint names or columns.
Using column names that are not keys for constraints.