0
0
MySQLquery~20 mins

Primary key declaration in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Primary Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this query?
Given the table users with columns id INT and name VARCHAR(50), and id declared as the primary key, what will this query return?

SELECT COUNT(*) FROM users WHERE id IS NULL;
AError: Cannot check NULL on primary key
B1
CNULL
D0
Attempts:
2 left
💡 Hint
Primary keys cannot have NULL values.
📝 Syntax
intermediate
2:00remaining
Which option correctly declares a primary key on the order_id column?
You want to create a table orders with a primary key on order_id. Which of the following CREATE TABLE statements is correct?
ACREATE TABLE orders (order_id INT PRIMARY KEY, order_date DATE);
BCREATE TABLE orders (order_id INT, order_date DATE, PRIMARY KEY (order_id));
CCREATE TABLE orders (order_id INT, order_date DATE, PRIMARY KEY order_id);
DCREATE TABLE orders (order_id INT, order_date DATE, PRIMARY order_id);
Attempts:
2 left
💡 Hint
The primary key constraint syntax requires parentheses around the column name when declared separately.
schema
advanced
2:00remaining
Which table schema correctly enforces a composite primary key on user_id and session_id?
You want to ensure that the combination of user_id and session_id is unique and not null. Which table definition achieves this?
ACREATE TABLE sessions (user_id INT, session_id INT, PRIMARY KEY (user_id, session_id));
BCREATE TABLE sessions (user_id INT, session_id INT UNIQUE, PRIMARY KEY (user_id));
CCREATE TABLE sessions (user_id INT PRIMARY KEY, session_id INT PRIMARY KEY);
DCREATE TABLE sessions (user_id INT, session_id INT, UNIQUE (user_id, session_id));
Attempts:
2 left
💡 Hint
Composite primary keys require listing all columns inside the PRIMARY KEY parentheses.
🔧 Debug
advanced
2:00remaining
Why does this table creation fail?
Consider this SQL statement:

CREATE TABLE products (product_id INT, name VARCHAR(100), PRIMARY KEY product_id);

Why does it cause an error?
AMissing parentheses around product_id in PRIMARY KEY declaration.
Bproduct_id must be declared as NOT NULL explicitly.
CPRIMARY KEY cannot be declared after columns.
DVARCHAR(100) is invalid syntax.
Attempts:
2 left
💡 Hint
Check the syntax for PRIMARY KEY constraints.
optimization
expert
3:00remaining
Which primary key choice optimizes query speed for a large table with many lookups by email?
You have a large customers table. You want to optimize queries that search by email. Which primary key declaration is best?
ANo primary key, just UNIQUE(email)
BPRIMARY KEY (customer_id)
CPRIMARY KEY (email)
DPRIMARY KEY (customer_id, email)
Attempts:
2 left
💡 Hint
Primary keys create clustered indexes that speed up lookups.