What does the AUTO_INCREMENT attribute do in MySQL?
It automatically generates a unique number for a column when a new row is inserted, usually used for primary keys.
Click to reveal answer
intermediate
Can you manually insert a value into an AUTO_INCREMENT column?
Yes, you can insert a specific value manually, but it must not conflict with existing values or the next auto-generated value.
Click to reveal answer
intermediate
What happens if you delete the last row with the highest AUTO_INCREMENT value?
The AUTO_INCREMENT counter does not decrease automatically; new inserts continue from the last highest value plus one.
Click to reveal answer
advanced
How can you reset the AUTO_INCREMENT counter in MySQL?
You can reset it using the command: ALTER TABLE table_name AUTO_INCREMENT = value; where value is the new starting number.
Click to reveal answer
advanced
Does AUTO_INCREMENT guarantee no gaps in the sequence of generated numbers?
No, gaps can occur due to deleted rows or rolled-back transactions, so AUTO_INCREMENT values are unique but not always consecutive.
Click to reveal answer
What is the default behavior of AUTO_INCREMENT in MySQL when inserting a new row?
AIt assigns the next available unique number automatically.
BIt requires you to provide a number manually.
CIt assigns a random number.
DIt assigns NULL.
✗ Incorrect
AUTO_INCREMENT automatically assigns the next unique number for the column.
If you delete the row with the highest AUTO_INCREMENT value, what happens to the next inserted row's AUTO_INCREMENT value?
AIt throws an error.
BIt reuses the deleted row's number.
CIt resets to 1.
DIt continues from the last highest value plus one.
✗ Incorrect
The counter does not decrease; it continues from the last highest value plus one.
How do you reset the AUTO_INCREMENT counter to start from 100 in a table named 'users'?
AALTER TABLE users AUTO_INCREMENT = 100;
BRESET AUTO_INCREMENT users TO 100;
CSET AUTO_INCREMENT users = 100;
DUPDATE users SET AUTO_INCREMENT = 100;
✗ Incorrect
The correct syntax is ALTER TABLE users AUTO_INCREMENT = 100;
Can AUTO_INCREMENT columns have gaps in their sequence?
ANo, they are always consecutive.
BOnly if you manually insert values.
CYes, gaps can occur due to deletions or rollbacks.
DOnly if the table is corrupted.
✗ Incorrect
Gaps can happen because of deleted rows or rolled-back transactions.
Which data type is commonly used with AUTO_INCREMENT columns?
ATEXT
BINTEGER
CVARCHAR
DDATE
✗ Incorrect
AUTO_INCREMENT is typically used with integer types to generate unique numbers.
Explain how AUTO_INCREMENT works in MySQL and what happens when rows are deleted.
Think about how MySQL keeps track of the next number to assign.
You got /4 concepts.
Describe how to reset the AUTO_INCREMENT value and why you might want to do that.
Consider the SQL command that changes table structure.
You got /3 concepts.
Practice
(1/5)
1. What does the AUTO_INCREMENT attribute do in a MySQL table?
easy
A. Automatically generates a unique number for each new row
B. Allows duplicate values in a column
C. Automatically deletes rows with duplicate values
D. Encrypts the column data
Solution
Step 1: Understand the purpose of AUTO_INCREMENT
AUTO_INCREMENT is used to create unique numbers automatically for new rows in a table, usually for primary keys.
Step 2: Compare options with the purpose
Only Automatically generates a unique number for each new row correctly describes this behavior; others describe unrelated features.
Final Answer:
Automatically generates a unique number for each new row -> Option A
Quick Check:
AUTO_INCREMENT = unique number generation [OK]
Hint: AUTO_INCREMENT means unique numbers auto-made for new rows [OK]
Common Mistakes:
Thinking AUTO_INCREMENT allows duplicates
Confusing AUTO_INCREMENT with encryption
Assuming AUTO_INCREMENT deletes rows
2. Which of the following is the correct syntax to create a table with an AUTO_INCREMENT column in MySQL?
easy
A. CREATE TABLE users (id INT AUTO_INCREMENT, name VARCHAR(50), PRIMARY KEY(name));
B. CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
C. CREATE TABLE users (id INT, name VARCHAR(50) AUTO_INCREMENT, PRIMARY KEY(id));
D. CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));
Solution
Step 1: Review correct AUTO_INCREMENT syntax
The AUTO_INCREMENT attribute must be on an integer column and usually combined with PRIMARY KEY.
Step 2: Check each option
CREATE TABLE users (id INT AUTO_INCREMENT, name VARCHAR(50), PRIMARY KEY(name)); misses PRIMARY KEY on the AUTO_INCREMENT column. CREATE TABLE users (id INT, name VARCHAR(50) AUTO_INCREMENT, PRIMARY KEY(id)); wrongly applies AUTO_INCREMENT to a VARCHAR column. CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50)); correctly combines AUTO_INCREMENT and PRIMARY KEY on an INT column. CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); lacks AUTO_INCREMENT.
Final Answer:
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50)); -> Option B
Quick Check:
Correct syntax = CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50)); [OK]
Hint: AUTO_INCREMENT must be on INT and PRIMARY KEY column [OK]
Common Mistakes:
Putting AUTO_INCREMENT on non-integer columns
Forgetting PRIMARY KEY on AUTO_INCREMENT column
Misplacing AUTO_INCREMENT after other column attributes
3. Given the table products created as:
CREATE TABLE products (product_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
and these inserts:
INSERT INTO products (name) VALUES ('Pen'); INSERT INTO products (name) VALUES ('Pencil'); INSERT INTO products (name) VALUES ('Eraser');
What will be the product_id of the 'Eraser' row?
medium
A. 1
B. 2
C. 3
D. NULL
Solution
Step 1: Understand AUTO_INCREMENT numbering
Each insert without specifying product_id gets the next number starting from 1.
Step 2: Count inserts before 'Eraser'
'Pen' gets 1, 'Pencil' gets 2, so 'Eraser' gets 3.
Final Answer:
3 -> Option C
Quick Check:
Third insert gets product_id 3 [OK]
Hint: AUTO_INCREMENT counts inserts starting at 1 by default [OK]
Which of the following statements will cause an error when inserting data?
medium
A. INSERT INTO orders (order_id, product) VALUES ('abc', 'Pencil');
B. INSERT INTO orders (order_id, product) VALUES (NULL, 'Pen');
C. INSERT INTO orders (order_id, product) VALUES (5, 'Notebook');
D. INSERT INTO orders (product) VALUES ('Book');
Solution
Step 1: Analyze each insert statement
INSERT INTO orders (product) VALUES ('Book'); inserts without order_id, so AUTO_INCREMENT assigns a number. INSERT INTO orders (order_id, product) VALUES (NULL, 'Pen'); inserts NULL for order_id, which is allowed and triggers AUTO_INCREMENT. INSERT INTO orders (order_id, product) VALUES (5, 'Notebook'); inserts a specific valid integer 5, allowed if unique. INSERT INTO orders (order_id, product) VALUES ('abc', 'Pencil'); tries to insert a string 'abc' into an INT column, causing a type error.
Step 2: Identify the error-causing statement
Only INSERT INTO orders (order_id, product) VALUES ('abc', 'Pencil'); causes a type mismatch error.
Final Answer:
INSERT INTO orders (order_id, product) VALUES ('abc', 'Pencil'); -> Option A
Quick Check:
Non-integer in INT column causes error [OK]
Hint: Only integers allowed in AUTO_INCREMENT column [OK]
Common Mistakes:
Thinking NULL is invalid for AUTO_INCREMENT
Assuming manual insert of valid integer always errors
Ignoring data type mismatch errors
5. A table employees has an AUTO_INCREMENT primary key emp_id. After inserting 10 rows, you delete the row with emp_id = 10. What happens when you insert a new row without specifying emp_id?
hard
A. The new row gets emp_id = 10 (reuses deleted ID)
B. The emp_id is set to NULL
C. The insert fails due to duplicate key
D. The new row gets emp_id = 11 (continues incrementing)
Solution
Step 1: Understand AUTO_INCREMENT behavior on deletions
Deleting a row does not reset or reuse the AUTO_INCREMENT counter.
Step 2: Determine next AUTO_INCREMENT value
After 10 inserts, the next value is 11, even if 10 was deleted.
Final Answer:
The new row gets emp_id = 11 (continues incrementing) -> Option D
Quick Check:
Deleted IDs are not reused by AUTO_INCREMENT [OK]
Hint: AUTO_INCREMENT never reuses deleted IDs, always increments [OK]
Common Mistakes:
Assuming AUTO_INCREMENT reuses deleted IDs
Expecting insert failure due to missing IDs
Thinking AUTO_INCREMENT resets automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.