Challenge - 5 Problems
Primary Key & SERIAL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this SERIAL column insertion?
Consider the table
users created with a SERIAL primary key. What will be the id values after inserting two rows without specifying id?PostgreSQL
CREATE TABLE users ( id SERIAL PRIMARY KEY, name TEXT ); INSERT INTO users (name) VALUES ('Alice'); INSERT INTO users (name) VALUES ('Bob'); SELECT id, name FROM users ORDER BY id;
Attempts:
2 left
💡 Hint
SERIAL automatically creates a sequence starting at 1 for the primary key.
✗ Incorrect
The SERIAL type creates an auto-incrementing integer starting at 1 by default. Each inserted row gets the next number automatically.
🧠 Conceptual
intermediate1:30remaining
Why is a PRIMARY KEY required to be unique?
Which of the following best explains why a PRIMARY KEY must be unique in a table?
Attempts:
2 left
💡 Hint
Think about how databases find a specific row quickly.
✗ Incorrect
A PRIMARY KEY uniquely identifies each row, so no duplicates are allowed. This ensures each row can be referenced precisely.
📝 Syntax
advanced2:00remaining
Which option correctly creates a table with a SERIAL primary key?
Choose the valid SQL statement to create a table
products with a SERIAL primary key column product_id and a text column product_name.Attempts:
2 left
💡 Hint
The SERIAL keyword implies integer type and auto-increment behavior.
✗ Incorrect
Option C correctly uses SERIAL with PRIMARY KEY. Option C has wrong order and type. Option C sets wrong primary key. Option C misses KEY keyword.
❓ optimization
advanced1:30remaining
What is the effect of using SERIAL vs manually creating a sequence for primary key?
Which statement about using SERIAL for primary keys compared to manually creating a sequence and setting default is true?
Attempts:
2 left
💡 Hint
Think about what SERIAL does behind the scenes.
✗ Incorrect
SERIAL is a shorthand that creates a sequence and sets the default value automatically. It also creates an index for the primary key.
🔧 Debug
expert2:30remaining
Why does this insertion fail with duplicate key error?
Given the table
orders with order_id SERIAL PRIMARY KEY, why does this insertion fail?
INSERT INTO orders (order_id, product) VALUES (1, 'Book');
Assuming a row with order_id = 1 already exists.Attempts:
2 left
💡 Hint
Think about primary key uniqueness and manual value insertion.
✗ Incorrect
Inserting a duplicate primary key value causes a uniqueness violation error. SERIAL allows manual insertion but uniqueness must be maintained.