0
0
SQLquery~10 mins

PRIMARY 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 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'
AFOREIGN KEY
BNOT NULL
CUNIQUE
DPRIMARY KEY
Attempts:
3 left
💡 Hint
Common Mistakes
Using FOREIGN KEY instead of PRIMARY KEY
Forgetting to specify PRIMARY KEY
Using UNIQUE which allows NULLs
2fill in blank
medium

Complete the code to add a primary key constraint named 'pk_order_id' on 'order_id'.

SQL
ALTER TABLE orders ADD CONSTRAINT [1] PRIMARY KEY (order_id);
Drag options to blanks, or click blank then click option'
Afk_order_id
Bunique_order_id
Cpk_order_id
Didx_order_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fk_' prefix which is for foreign keys
Using 'unique_' which is for unique constraints
Using 'idx_' which is for indexes
3fill in blank
hard

Fix the error in the code to correctly declare a composite primary key on 'id' and 'email'.

SQL
CREATE TABLE contacts (id INT, email VARCHAR(255), [1] pk_contacts PRIMARY KEY (id, email));
Drag options to blanks, or click blank then click option'
ACONSTRAINT
BINDEX
CFOREIGN KEY
DUNIQUE
Attempts:
3 left
💡 Hint
Common Mistakes
Using INDEX instead of CONSTRAINT
Using FOREIGN KEY which is for references
Using UNIQUE which is not a primary key
4fill in blank
hard

Fill both blanks to create a table 'products' with 'product_id' as primary key and 'name' not null.

SQL
CREATE TABLE products (product_id INT [1], name VARCHAR(100) [2]);
Drag options to blanks, or click blank then click option'
APRIMARY KEY
BFOREIGN KEY
CNOT NULL
DUNIQUE
Attempts:
3 left
💡 Hint
Common Mistakes
Using FOREIGN KEY instead of PRIMARY KEY
Missing NOT NULL on important columns
5fill in blank
hard

Fill all three blanks to add a primary key constraint named 'pk_emp' on 'emp_id' in the 'employees' table.

SQL
ALTER TABLE employees ADD [1] pk_emp [2] [3] (emp_id);
Drag options to blanks, or click blank then click option'
ACONSTRAINT
BPRIMARY
CKEY
DINDEX
Attempts:
3 left
💡 Hint
Common Mistakes
Using INDEX instead of CONSTRAINT
Omitting PRIMARY or KEY keywords