JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Reset Auto Increment in MySQL

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

This tutorial shows you how to reset the auto increment in a MySQL table.

Most businesses and organizations using MySQL need to insert values ​​into multiple tables simultaneously. While it is not possible to make all fields of the inserted records non-null, it may cause errors or raise errors when retrieving these records based on specific conditions.

To solve this problem, MySQL provides us with an auto increment field. It helps us to add or update a specific value in MySQL every time we insert a new record. It is usually used for columns associated with primary keys.

As the value of this variable increases, the analyst must control the value of this variable. This can be ALTER TABLEdone using the statement.

Let us try to understand this a little deeper.

However, before we get started, we'll create two virtual tables to work with. Here, we create a table student_dates_1, and a few rows.

-- create the table student_dates_3
CREATE TABLE student_dates_3(
  stu_id int NOT NULL AUTO_INCREMENT,
  stu_firstName varchar(255) DEFAULT NULL,
  stu_date date,
  primary key(stu_id)
);

Use INSERTthe statement student_datesto insert an entry into the table.

The previous query created a student_datestable called . Now INSERTwith the help of the statement, let's try to add data for some students. This can be done as follows:

-- insert rows to the table student_dates_3
INSERT INTO student_dates_3(stu_firstName,stu_date) 
 VALUES("Preet",STR_TO_DATE('24-May-2005', '%d-%M-%Y')),
 ("Dhruv",STR_TO_DATE('14-June-2001', '%d-%M-%Y')),
 ("Mathew",STR_TO_DATE('13-December-2020', '%d-%M-%Y')),
 ("Jeet",STR_TO_DATE('14-May-2003', '%d-%M-%Y')),
 ("Steyn",STR_TO_DATE('19-July-2002', '%d-%M-%Y'));

The above code will student_datesenter the student data in the table.

SELECT * from student_dates_3;

Output:

stu_id	stu_firstName	stu_date
1		Preet			2005-05-24
2		Dhruv			2001-06-14
3		Mathew			2020-12-13
4		Jeet			2003-05-14
5		Steyn			2002-07-19

Reset AUTO_INCREMENTthe value of

To AUTO_INCREMENTupdate the value of a variable to a specific value, you can use the following syntax.

ALTER TABLE name_of_the_table AUTO_INCREMENT = x;

In the above query, xrepresents the updated values ​​that need to be added to the said table. This logic can be used to update student_dates_3the variable values ​​of a table with the help of the following query.

ALTER TABLE student_dates_3 AUTO_INCREMENT = 100;

If we add a record, the primary key stu_idis automatically updated to AUTO_INCREMENTthe value set in the variable. Now let's student_dates_3insert a value into the table.

-- insert rows to the table student_dates_3
INSERT INTO student_dates_3(stu_firstName,stu_date) 
 VALUES("Rutvik",STR_TO_DATE('16-January-2001', '%d-%M-%Y'));

We set the for Rutvikthe student named to . This can be checked with the following query.stu_id100

SELECT * from student_dates_3;

Output:

stu_id	stu_firstName	stu_date
1		Preet			2005-05-24
2		Dhruv			2001-06-14
3		Mathew			2020-12-13
4		Jeet			2003-05-14
5		Steyn			2002-07-19
6		Rutvik			2001-16-01

Therefore, with the help of ALTER TABLEthe INSERT statement and AUTO_INCREMENTthe UPDATE keyword, we can effectively update AUTO_INCREMENTthe value of the field, thereby automatically updating the values ​​of the records in the columns associated with the primary key.

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

If ELSE in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering

DATETIME vs. TIMESTAMP in MySQL

Publish Date:2025/04/11 Views:117 Category:MySQL

DATETIME and TIMESTAMP are two different data types that can be used to store values ​​that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Joining 3 tables in MySQL

Publish Date:2025/04/11 Views:187 Category:MySQL

In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS

Use of UPDATE JOIN in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla

How to use the Row_Number() function in MySQL

Publish Date:2025/04/11 Views:142 Category:MySQL

In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti

Multiple primary keys in MySQL

Publish Date:2025/04/11 Views:66 Category:MySQL

In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b

Displaying foreign keys in MySQL

Publish Date:2025/04/11 Views:55 Category:MySQL

In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei

Select first N rows in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial