A serial column is a special integer column that automatically increments its value for each new row. It uses a sequence behind the scenes to generate unique numbers.
Click to reveal answer
intermediate
How does an identity column differ from a serial column?
An identity column is part of the SQL standard and provides automatic numbering like serial, but it is more flexible and preferred in modern PostgreSQL versions. It can be defined as GENERATED ALWAYS or GENERATED BY DEFAULT.
Click to reveal answer
beginner
What SQL syntax creates a serial column?
Example: CREATE TABLE users (id serial PRIMARY KEY, name text); This creates an integer column id that auto-increments with each new row.
Click to reveal answer
intermediate
Write the SQL to create an identity column named id that always generates a new value.
Example: CREATE TABLE users (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, name text); This creates an id column that automatically generates unique values and does not allow manual inserts.
Click to reveal answer
intermediate
Why might you choose identity columns over serial columns?
Because identity columns follow the SQL standard, offer more control, and avoid creating implicit sequences, making your database design cleaner and more portable.
Click to reveal answer
Which PostgreSQL column type automatically creates a sequence and increments values?
Aserial
Bvarchar
Ctext
Dboolean
✗ Incorrect
The serial type automatically creates a sequence and increments integer values for new rows.
What keyword is used to define an identity column that always generates a value?
AGENERATED BY DEFAULT
BGENERATED ALWAYS
CAUTO_INCREMENT
DSERIAL
✗ Incorrect
GENERATED ALWAYS AS IDENTITY means the database always generates the value automatically.
Which of these is a benefit of using identity columns over serial columns?
AThey are part of the SQL standard
BThey allow text values
CThey do not auto-increment
DThey require manual sequence creation
✗ Incorrect
Identity columns follow the SQL standard, making them more portable and standardized.
What happens if you insert a row without specifying a value for a serial column?
AYou must provide a value manually
BThe insert fails with an error
CThe column is set to NULL
DThe database generates the next number automatically
✗ Incorrect
Serial columns auto-increment, so the database fills in the next number automatically.
Which statement is true about GENERATED BY DEFAULT AS IDENTITY?
AIt is the same as <code>serial</code>
BThe database always generates the value and disallows manual inserts
CYou can insert your own value or let the database generate one
DIt disables auto-increment
✗ Incorrect
GENERATED BY DEFAULT allows manual insertion of values but generates one if none is provided.
Explain the difference between serial and identity columns in PostgreSQL.
Think about how the numbering is generated and standards.
You got /4 concepts.
Describe how to create a table with an auto-incrementing primary key using identity columns.
Focus on the syntax for identity columns.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of using SERIAL or IDENTITY columns in PostgreSQL?
easy
A. To automatically generate unique numbers for new rows
B. To store large text data efficiently
C. To create foreign key relationships
D. To improve query performance by indexing
Solution
Step 1: Understand the role of SERIAL and IDENTITY
Both SERIAL and IDENTITY columns are designed to automatically generate unique numeric values for new rows in a table.
Step 2: Identify the main purpose
This automatic numbering helps avoid manual entry and ensures unique identifiers for each row.
Final Answer:
To automatically generate unique numbers for new rows -> Option A
Quick Check:
Serial and identity columns = auto unique numbers [OK]
Hint: They auto-generate unique IDs for new rows [OK]
Common Mistakes:
Thinking SERIAL stores text data
Confusing SERIAL with indexing
Assuming SERIAL creates foreign keys
2.
Which of the following is the correct syntax to create a table with an IDENTITY column named id in PostgreSQL?
CREATE TABLE users (
id ???,
name TEXT
);
easy
A. INT GENERATED AS IDENTITY
B. SERIAL PRIMARY KEY
C. INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
D. INTEGER AUTO_INCREMENT PRIMARY KEY
Solution
Step 1: Recall IDENTITY syntax in PostgreSQL
The correct syntax uses GENERATED ALWAYS AS IDENTITY for identity columns.
Step 2: Match the option with correct syntax
INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY uses INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, which is valid PostgreSQL syntax.
Final Answer:
INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY -> Option C
Quick Check:
IDENTITY syntax = GENERATED ALWAYS AS IDENTITY [OK]
Hint: IDENTITY uses GENERATED ALWAYS AS IDENTITY syntax [OK]
Common Mistakes:
Using AUTO_INCREMENT (MySQL syntax)
Confusing SERIAL with IDENTITY syntax
Omitting GENERATED ALWAYS or BY DEFAULT
3.
Given the table creation:
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
name TEXT
);
INSERT INTO products (name) VALUES ('Pen'), ('Pencil'), ('Eraser');
SELECT product_id FROM products ORDER BY product_id;
What will be the output of the SELECT query?
medium
A. [3, 2, 1]
B. [0, 1, 2]
C. [NULL, NULL, NULL]
D. [1, 2, 3]
Solution
Step 1: Understand SERIAL behavior on insert
The SERIAL column auto-increments starting from 1 for each inserted row.
Step 2: Check inserted rows and ordering
Three rows inserted get product_id values 1, 2, and 3 respectively. Ordering by product_id returns them in ascending order.
Final Answer:
[1, 2, 3] -> Option D
Quick Check:
SERIAL starts at 1 and increments [OK]
Hint: SERIAL starts numbering at 1 by default [OK]
Common Mistakes:
Assuming numbering starts at 0
Expecting NULL values for SERIAL
Thinking order is descending by default
4.
Consider this table creation:
CREATE TABLE orders (
order_id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
product TEXT
);
Which of the following statements will cause an error when inserting data?
medium
A. INSERT INTO orders (order_id, product) VALUES (5, 'Pen'), (5, 'Pencil');
B. INSERT INTO orders (order_id, product) VALUES (5, 'Pen');
C. INSERT INTO orders (product) VALUES ('Book');
D. INSERT INTO orders (order_id, product) VALUES (DEFAULT, 'Notebook');
Solution
Step 1: Understand GENERATED BY DEFAULT AS IDENTITY behavior
This allows manual insertion of order_id or automatic generation if omitted.
Step 2: Analyze each insert statement
INSERT INTO orders (order_id, product) VALUES (5, 'Pen'), (5, 'Pencil'); tries to insert two rows with the same order_id = 5, violating the primary key uniqueness constraint, causing an error.
Final Answer:
INSERT with duplicate order_id values causes error -> Option A
Quick Check:
Duplicate primary keys cause insert errors [OK]
Hint: Duplicate primary keys cause insert errors [OK]
Common Mistakes:
Assuming GENERATED BY DEFAULT forbids manual IDs
Ignoring primary key uniqueness
Thinking DEFAULT keyword causes errors
5.
You want to create a table employees with a IDENTITY column named id that automatically generates unique IDs but also allows you to insert specific IDs manually when needed. Which is the best way to define the ID column?
hard
A. Use SERIAL PRIMARY KEY for the ID column
B. Use INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
C. Use INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
D. Use INTEGER without any auto-generation
Solution
Step 1: Understand difference between GENERATED ALWAYS and BY DEFAULT
GENERATED ALWAYS AS IDENTITY disallows manual inserts; BY DEFAULT allows manual ID insertion.
Step 2: Match requirement to syntax
Since manual insertion is needed sometimes, GENERATED BY DEFAULT AS IDENTITY is the best choice.
Final Answer:
INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY -> Option B
Quick Check:
BY DEFAULT allows manual IDs, ALWAYS does not [OK]
Hint: BY DEFAULT allows manual ID insertion [OK]
Common Mistakes:
Choosing GENERATED ALWAYS which forbids manual IDs
Using SERIAL which is less flexible
Not using any auto-generation and risking duplicates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.