MySQL
database
auto increment
initial value
SQL tutorial

How to set initial value and auto increment in MySQL?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In this article, we will explore how to set an initial value and enable auto-increment in MySQL. This feature is crucial when designing and maintaining databases that require unique identifiers, typically for primary keys in tables. Understanding and implementing auto-increment is foundational for database management.

Understanding Auto Increment in MySQL

Auto-increment is a feature that automatically generates a unique number when a new record is inserted into a table. This feature is typically used for primary keys, which need to be unique and not null. The auto-increment feature ensures that each new record gets a distinct number without requiring manual input.

Technical Explanation

When creating a table, you can specify a column with the AUTO_INCREMENT attribute. This notifies MySQL to automatically increment the value of the column with each new row added. The subsequent entry gets the next integer value, ensuring uniqueness.

Setting Up Auto Increment

To implement auto-increment, you'll need to define it while creating your table or while modifying an existing one. Here’s how you can do it:

Creating a Table with Auto Increment

Below is an example of creating a new table where the id column uses AUTO_INCREMENT.

sql
1CREATE TABLE users (
2    id INT NOT NULL AUTO_INCREMENT,
3    name VARCHAR(100) NOT NULL,
4    email VARCHAR(100) NOT NULL,
5    PRIMARY KEY (id)
6);

Modifying a Table to Add Auto Increment

If your table already exists and you need to set a column to auto-increment, you can do it using the ALTER TABLE statement:

sql
ALTER TABLE users MODIFY id INT NOT NULL AUTO_INCREMENT;

Setting an Initial Value for Auto Increment

MySQL allows you to modify the point where auto-incrementation begins using the AUTO_INCREMENT table option. By default, this starts at 1. If you want to start this sequence at a different number, such as 1001, note the following:

  1. During Table Creation:
sql
1   CREATE TABLE products (
2       product_id INT NOT NULL AUTO_INCREMENT,
3       name VARCHAR(100) NOT NULL,
4       PRIMARY KEY (product_id)
5   ) AUTO_INCREMENT=1001;
  1. Modifying an Existing Table:
sql
   ALTER TABLE products AUTO_INCREMENT = 1001;

Important Considerations

  • Atomicity: Auto-increment values guarantee atomicity similar to a transaction, meaning it's thread-safe, and no two threads can increment the same value simultaneously.
  • Gaps: Gaps can occur if a transaction that generates an auto-increment number is rolled back, or if DELETE operations are utilized.
  • Max Value: The maximum permissible value for an auto-increment column depends on the data type (e.g., INT has a max value of 23112^{31}-1 for unsigned).

Practical Examples

Consider a scenario where you have an orders table:

sql
1CREATE TABLE orders (
2    order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
3    order_date DATE NOT NULL,
4    customer_id INT NOT NULL,
5    PRIMARY KEY (order_id)
6) AUTO_INCREMENT=5000;

In this example, the order_id will start at 5000 and increment by 1 for each new order added.

Summary Table

ConceptDescription
AUTO_INCREMENTEnsures automatic increment of primary keys with each new insert.
Default Starting Value1 (can be changed using AUTO_INCREMENT option).
How to Set Initial ValueUse AUTO_INCREMENT=<initial value> during table creation or modification.
Column Type RequiredTypically an integer type, but can vary based on design.
Maximum ValueDepends on the data type of the column (e.g., 23112^{31}-1 for unsigned INT).
Atomicity & GapsAuto-incrementation is atomic; gaps may appear if transactions roll back.

Conclusion

Using the AUTO_INCREMENT attribute in MySQL is a standard practice for ensuring unique primary keys. Whether you’re setting up a new table or updating an existing one, understanding how to configure and manipulate the initial value of auto-increment properties is essential for database design and integrity. Remember to consider potential gaps and database needs when implementing this feature.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.