JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Adding an auto-increment column in MySQL

Author:JIYIK Last Updated:2025/04/23 Views:

In this article, we will learn how to add an auto-increment column in MySQL.


Adding an auto-increment column in MySQL

When creating a table, we may not have a unique identifier in the database, which brings problems to choosing the primary key. To solve such problems, we have to manually assign a unique key to each record, which is usually time-consuming.

Therefore, we might use the auto-increment feature, which creates a numeric primary key value for each new record added.

AUTO_INCREMENTKeyword used for the auto-increment feature in MySQL. AUTO_INCREMENTStarts at 1 and increases by 1 by default.

To further understand the previous concepts, consider the following example.

Example 1: Add a column to a new table.

CREATE TABLE Employees(
    employee_id INT AUTO_INCREMENT PRIMARY KEY,
    first_Name VARCHAR(255),
    last_Name VARCHAR(255)
);

In the above example, we have created an Employees table with the fields employee_id, first_Name and last_Name. This will automatically generate employee_id using AUTO_INCREMENT and make it the primary key of the database.

Example 2: Modify the columns of an existing table.

ALTER TABLE `Employees`
    MODIFY COLUMN `employee_id` INT NOT NULL AUTO_INCREMENT;

In the previous example, we already have an Employees table with fields employee_id, first_Name, and last_Name. We will use AUTO_INCREMENT to generate employee_id by changing the existing properties of the employee_id column.

Because it is automatically inserted, we do not need to specify the value of employee_id when adding a new record to the Employees table. The key of the initial record will be 1, and the key of each subsequent record will increase by 1.

INSERT INTO Employees(first_Name, last_Name)
VALUES ('John', 'Doe'),
VALUES ('Smith', 'Will'),
VALUES ('Tony', 'Stark');

We can modify the default starting value using the ALTER TABLE command as shown below:

ALTER TABLE Employees AUTO_INCREMENT = new_value;

We can set new_value to the default starting value like this:

ALTER TABLE Employees AUTO_INCREMENT = 1001;

In this case, the initial value we want to use is new_value. We give the MySQL server variable AUTO_INCREMENTa new interval value by AUTO_INCREMENTchanging the interval value to a number other than 1.

For our example, we have set this number to 1001.

Run the above line of code in any browser compatible with MySQL. It will display the following result:

MySQL add auto-increment column

After modifying the default starting value:

MySQL update auto-increment column

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Display tables and database structure in MySQL

Publish Date:2025/04/23 Views:97 Category:MySQL

Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi

Select first row from MySQL table

Publish Date:2025/04/23 Views:112 Category:MySQL

Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic

Insert timestamp into MySQL table

Publish Date:2025/04/23 Views:77 Category:MySQL

Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T

The difference between two tables in MySQL

Publish Date:2025/04/23 Views:102 Category:MySQL

In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta

MySQL sorts data alphabetically

Publish Date:2025/04/23 Views:129 Category:MySQL

In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t

Display the current database in MySQL

Publish Date:2025/04/23 Views:199 Category:MySQL

This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query

Check if a database exists in MySQL

Publish Date:2025/04/23 Views:179 Category:MySQL

In this article, we will introduce many ways to check if a database exists in MySQL. Check if the database exists in MySQL The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL serve

Get the sum of multiple columns in MySQL

Publish Date:2025/04/23 Views:125 Category:MySQL

In this article, we will learn how to sum multiple columns in MySQL. Sum multiple columns in MySQL You can use aggregate functions SUM() to calculate the total value in a collection. SUM() The function calculation does not consider NULL val

MySQL ForEach Loop

Publish Date:2025/04/23 Views:164 Category:MySQL

This article describes how to simulate a foreach loop in MySQL using INSERT, SELECT, WHERE, and JOIN in one statement. MySQL foreach loop To understand foreach loop simulation, let us create three tables with the following names and attribu

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial