0
0
PostgreSQLquery~20 mins

PRIMARY KEY and SERIAL behavior in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Primary Key & SERIAL 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 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;
A[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]
B[{"id":null,"name":"Alice"},{"id":null,"name":"Bob"}]
C[{"id":0,"name":"Alice"},{"id":1,"name":"Bob"}]
D[{"id":100,"name":"Alice"},{"id":101,"name":"Bob"}]
Attempts:
2 left
💡 Hint
SERIAL automatically creates a sequence starting at 1 for the primary key.
🧠 Conceptual
intermediate
1: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?
ATo speed up data insertion by avoiding uniqueness checks.
BTo uniquely identify each row so that no two rows have the same key value.
CTo enable multiple rows to share the same key for grouping.
DTo allow NULL values in the key column.
Attempts:
2 left
💡 Hint
Think about how databases find a specific row quickly.
📝 Syntax
advanced
2: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.
ACREATE TABLE products (product_id SERIAL, product_name TEXT, PRIMARY KEY (product_name));
BCREATE TABLE products (product_id INTEGER SERIAL PRIMARY KEY, product_name TEXT);
CCREATE TABLE products (product_id SERIAL PRIMARY KEY, product_name TEXT);
DCREATE TABLE products (product_id SERIAL PRIMARY, product_name TEXT);
Attempts:
2 left
💡 Hint
The SERIAL keyword implies integer type and auto-increment behavior.
optimization
advanced
1: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?
AManually creating a sequence is faster than using SERIAL for inserts.
BSERIAL requires manual sequence creation and default setting.
CUsing SERIAL disables indexing on the primary key column.
DSERIAL automatically creates a sequence and sets the default, simplifying table creation.
Attempts:
2 left
💡 Hint
Think about what SERIAL does behind the scenes.
🔧 Debug
expert
2: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.
ABecause manually inserting a value that duplicates an existing primary key violates uniqueness.
BBecause SERIAL columns cannot be inserted manually at all.
CBecause the product column is missing a NOT NULL constraint.
DBecause the sequence for SERIAL resets after each insert.
Attempts:
2 left
💡 Hint
Think about primary key uniqueness and manual value insertion.