Bird
Raised Fist0
SQLquery~10 mins

AUTO_INCREMENT vs SERIAL vs IDENTITY in SQL - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - AUTO_INCREMENT vs SERIAL vs IDENTITY
Start: Insert new row
↓
Check if PK value given?
Yes→Use given value
No↓
Generate next unique number
↓
Assign number to PK column
↓
Insert row with PK
↓
End
When inserting a new row, the database checks if a primary key (PK) value is given. If not, it generates the next unique number automatically using AUTO_INCREMENT, SERIAL, or IDENTITY depending on the database.
Execution Sample
SQL
CREATE TABLE users (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(50)
);

INSERT INTO users (name) VALUES ('Alice');
INSERT INTO users (name) VALUES ('Bob');
This code creates a table with an auto-incrementing id and inserts two rows, letting the database assign ids automatically.
Execution Table
StepActionPK value given?PK assignedResulting row
1Insert ('Alice')No1id=1, name='Alice'
2Insert ('Bob')No2id=2, name='Bob'
3Insert with id=5Yes5id=5, name='Custom'
4Insert ('Charlie')No6id=6, name='Charlie'
💡 Insertion stops when no more rows to insert.
Variable Tracker
VariableStartAfter 1After 2After 3After 4
Next PK value12367
Key Moments - 2 Insights
Why does the next PK value jump from 3 to 6 after inserting a row with id=5?
Because manually inserting id=5 sets the sequence to start after 5, so the next auto-generated PK becomes 6, as shown in execution_table row 4.
What happens if I provide a PK value that already exists?
The database will raise an error due to primary key conflict, preventing duplicate PKs. This is not shown in the table but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what PK value is assigned to the second inserted row?
A1
B2
C3
D5
💡 Hint
Check the 'PK assigned' column in row 2 of the execution_table.
At which step does the PK value get manually assigned instead of auto-generated?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for 'PK value given?' marked as 'Yes' in the execution_table.
If you insert a new row after step 4, what will be the next PK value assigned?
A7
B6
C5
D8
💡 Hint
Check the 'Next PK value' in variable_tracker after step 4.
Concept Snapshot
AUTO_INCREMENT, SERIAL, and IDENTITY are ways databases auto-generate unique numbers for primary keys.
AUTO_INCREMENT is common in MySQL.
SERIAL is a PostgreSQL shortcut for auto-incrementing integers.
IDENTITY is the SQL standard way used in SQL Server and others.
They all assign unique numbers automatically when no PK is given.
Manual PK values can override and affect the next auto-generated number.
Full Transcript
When you add a new row to a table with an auto-incrementing primary key, the database checks if you gave a primary key value. If you did, it uses that value. If not, it generates the next unique number automatically. AUTO_INCREMENT is used in MySQL, SERIAL in PostgreSQL, and IDENTITY in SQL Server. For example, inserting 'Alice' and 'Bob' without specifying ids assigns 1 and 2 automatically. If you insert a row with id=5 manually, the next auto-generated id will jump to 6. This ensures no duplicate primary keys. Understanding this helps you manage keys and avoid conflicts when inserting data.

Practice

(1/5)
1. Which keyword is used in MySQL to automatically generate unique numbers for a primary key column?
easy
A. IDENTITY
B. SERIAL
C. SEQUENCE
D. AUTO_INCREMENT

Solution

  1. Step 1: Understand MySQL auto-numbering

    MySQL uses AUTO_INCREMENT to generate unique numbers automatically for columns like primary keys.
  2. Step 2: Compare with other keywords

    SERIAL is PostgreSQL-specific, IDENTITY is SQL standard used in SQL Server and newer PostgreSQL versions, and SEQUENCE is a separate object, not a column keyword.
  3. Final Answer:

    AUTO_INCREMENT -> Option D
  4. Quick Check:

    MySQL auto-number = AUTO_INCREMENT [OK]
Hint: MySQL uses AUTO_INCREMENT for auto numbering [OK]
Common Mistakes:
  • Confusing SERIAL with MySQL syntax
  • Using IDENTITY in MySQL
  • Thinking SEQUENCE is a column keyword
2. Which of the following is the correct syntax to create an auto-incrementing primary key column in PostgreSQL using the SERIAL keyword?
easy
A. CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50));
B. CREATE TABLE users (id AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
C. CREATE TABLE users (id IDENTITY PRIMARY KEY, name VARCHAR(50));
D. CREATE TABLE users (id SEQUENCE PRIMARY KEY, name VARCHAR(50));

Solution

  1. Step 1: Identify PostgreSQL SERIAL syntax

    In PostgreSQL, SERIAL is used as a shorthand to create an integer column that auto-increments.
  2. Step 2: Check syntax correctness

    CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50)); uses id SERIAL PRIMARY KEY, which is valid. Options B and C use MySQL and SQL Server syntax incorrectly in PostgreSQL. CREATE TABLE users (id SEQUENCE PRIMARY KEY, name VARCHAR(50)); uses SEQUENCE incorrectly as a column type.
  3. Final Answer:

    CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50)); -> Option A
  4. Quick Check:

    SERIAL syntax in PostgreSQL = CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50)); [OK]
Hint: PostgreSQL uses SERIAL for auto-increment columns [OK]
Common Mistakes:
  • Using AUTO_INCREMENT in PostgreSQL
  • Using IDENTITY without parentheses
  • Treating SEQUENCE as a column type
3. Given the following SQL Server table creation statement, what will be the output of inserting two rows without specifying the ID column?
CREATE TABLE products (id INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(20));
INSERT INTO products (name) VALUES ('Pen');
INSERT INTO products (name) VALUES ('Pencil');
SELECT * FROM products;
medium
A. [{"id": null, "name": "Pen"}, {"id": null, "name": "Pencil"}]
B. [{"id": 0, "name": "Pen"}, {"id": 1, "name": "Pencil"}]
C. [{"id": 1, "name": "Pen"}, {"id": 2, "name": "Pencil"}]
D. Syntax error on INSERT statements

Solution

  1. Step 1: Understand IDENTITY behavior in SQL Server

    The IDENTITY(1,1) means the ID column starts at 1 and increments by 1 automatically for each new row.
  2. Step 2: Predict inserted values

    Inserting two rows without specifying ID will assign IDs 1 and 2 respectively. The SELECT will show these IDs with names.
  3. Final Answer:

    [{"id": 1, "name": "Pen"}, {"id": 2, "name": "Pencil"}] -> Option C
  4. Quick Check:

    IDENTITY auto increments IDs starting at 1 [OK]
Hint: IDENTITY(1,1) starts numbering from 1 incrementing by 1 [OK]
Common Mistakes:
  • Assuming ID starts at 0
  • Expecting null IDs without insert values
  • Thinking INSERT needs ID value explicitly
4. You try to create a table in PostgreSQL with this statement:
CREATE TABLE orders (order_id INT AUTO_INCREMENT PRIMARY KEY, order_date DATE);

What is the error and how to fix it?
medium
A. AUTO_INCREMENT is invalid in PostgreSQL; use SERIAL or IDENTITY instead
B. Missing parentheses after AUTO_INCREMENT
C. order_id must be VARCHAR for AUTO_INCREMENT
D. No error; statement is correct

Solution

  1. Step 1: Identify AUTO_INCREMENT support

    PostgreSQL does not support AUTO_INCREMENT; this is a MySQL keyword.
  2. Step 2: Correct PostgreSQL syntax

    Use SERIAL or GENERATED AS IDENTITY for auto-incrementing columns in PostgreSQL.
  3. Final Answer:

    AUTO_INCREMENT is invalid in PostgreSQL; use SERIAL or IDENTITY instead -> Option A
  4. Quick Check:

    PostgreSQL uses SERIAL or IDENTITY, not AUTO_INCREMENT [OK]
Hint: PostgreSQL rejects AUTO_INCREMENT; use SERIAL or IDENTITY [OK]
Common Mistakes:
  • Trying to use MySQL syntax in PostgreSQL
  • Assuming AUTO_INCREMENT works everywhere
  • Ignoring error messages about syntax
5. You want to migrate a MySQL table with AUTO_INCREMENT primary key to PostgreSQL. Which approach correctly preserves auto-increment behavior?
hard
A. Keep AUTO_INCREMENT keyword as is in PostgreSQL
B. Replace AUTO_INCREMENT with SERIAL type for the primary key column
C. Use IDENTITY without specifying start value
D. Manually insert IDs for each row without auto-increment

Solution

  1. Step 1: Understand MySQL to PostgreSQL migration

    MySQL uses AUTO_INCREMENT, but PostgreSQL does not support it directly.
  2. Step 2: Use PostgreSQL equivalent

    PostgreSQL uses SERIAL as a shorthand for auto-increment integer columns, preserving behavior.
  3. Final Answer:

    Replace AUTO_INCREMENT with SERIAL type for the primary key column -> Option B
  4. Quick Check:

    MySQL AUTO_INCREMENT -> PostgreSQL SERIAL [OK]
Hint: Use SERIAL in PostgreSQL to replace MySQL AUTO_INCREMENT [OK]
Common Mistakes:
  • Leaving AUTO_INCREMENT keyword unchanged
  • Using IDENTITY without proper syntax
  • Manually managing IDs instead of auto-increment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard