0
0
SQLquery~20 mins

PRIMARY KEY constraint in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Primary Key Master
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 on a table with PRIMARY KEY?
Given a table Users with a PRIMARY KEY on column user_id, what will happen if you try to insert two rows with the same user_id?
SQL
CREATE TABLE Users (
  user_id INT PRIMARY KEY,
  username VARCHAR(50)
);

INSERT INTO Users (user_id, username) VALUES (1, 'Alice');
INSERT INTO Users (user_id, username) VALUES (1, 'Bob');
AThe second insert will fail with a duplicate key error.
BBoth inserts will succeed, creating two rows with user_id = 1.
CThe second insert will overwrite the first row with user_id = 1.
DThe database will ignore the second insert silently.
Attempts:
2 left
💡 Hint
PRIMARY KEY enforces uniqueness for the column.
🧠 Conceptual
intermediate
1:30remaining
Which statement about PRIMARY KEY is true?
Choose the correct statement about the PRIMARY KEY constraint in SQL.
APRIMARY KEY columns cannot contain NULL values.
BA table can have multiple PRIMARY KEY columns.
CPRIMARY KEY allows duplicate values but no NULLs.
DPRIMARY KEY columns can contain NULL values if specified.
Attempts:
2 left
💡 Hint
Think about uniqueness and nullability rules for PRIMARY KEY.
📝 Syntax
advanced
2:00remaining
Which CREATE TABLE statement correctly defines a composite PRIMARY KEY?
Select the valid SQL syntax to create a table with a composite PRIMARY KEY on columns order_id and product_id.
A
CREATE TABLE Orders (
  order_id INT PRIMARY KEY,
  product_id INT PRIMARY KEY
);
B
CREATE TABLE Orders (
  order_id INT,
  product_id INT,
  PRIMARY KEY (order_id, product_id)
);
C
CREATE TABLE Orders (
  order_id INT,
  product_id INT,
  PRIMARY KEY order_id, product_id
);
D
CREATE TABLE Orders (
  order_id INT,
  product_id INT,
  PRIMARY KEY = (order_id, product_id)
);
Attempts:
2 left
💡 Hint
Composite keys are defined with parentheses and commas inside PRIMARY KEY clause.
🔧 Debug
advanced
2:00remaining
Why does this table creation fail?
This SQL code fails to create the table. What is the reason?
SQL
CREATE TABLE Employees (
  emp_id INT,
  emp_name VARCHAR(100),
  PRIMARY KEY emp_id
);
Aemp_name must be part of the PRIMARY KEY.
Bemp_id cannot be PRIMARY KEY because it is INT type.
CPRIMARY KEY clause is missing parentheses around emp_id.
DPRIMARY KEY cannot be defined inside CREATE TABLE.
Attempts:
2 left
💡 Hint
Check the syntax for PRIMARY KEY declaration.
optimization
expert
2:30remaining
How does a PRIMARY KEY affect query performance?
Which statement best describes how a PRIMARY KEY constraint impacts database query performance?
APRIMARY KEY forces full table scans for all queries involving that column.
BPRIMARY KEY slows down queries because it adds extra checks on every SELECT.
CPRIMARY KEY has no effect on query performance, only on data integrity.
DPRIMARY KEY automatically creates a unique index, speeding up searches on that column.
Attempts:
2 left
💡 Hint
Think about how databases use indexes.