Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

How to reset auto increment values in MySQL

23 September 2020

Preamble

SQLS*Plus - 1649771985171370CCED0 ED29 478C B748 28A392C69B2C%202 optimize

​​

MySQL provides you with a useful function called auto increment. You can assign the AUTO_INCREMENT attribute to the table column to create a unique identifier for the new row. Generally, you use the AUTO_INCREMENT attribute for the primary table key column.

Whenever you insert a new row into a table, MySQL uses the AUTO_INCREMENT attribute to automatically assign an ordinal number to the column.

For example, if there are eight rows in a table and you insert a new row without specifying a value for the autoincrement column, MySQL will automatically insert a new id row with a value of 9.

Sometimes you may need to reset the autoincrement column value so that the ID of the first record you insert into the table starts with a certain number, for example, 1.

In MySQL you can reset the auto-increment values in various ways.

Examples of resetting the value of automatic increments in MySQL

First, create a table with the name tmp and assign the attribute AUTO_INCREMENT to the id column of the primary key.

CREATE TABLE tmp (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(45) DEFAULT NULL,
PRIMARY KEY (id)
);

Second, insert a sample data in the tmp table:

INSERT INTO tmp(name)
VALUES('test 1'),
('test 2'),
('test 3');

Thirdly, a query to the tmp table to check the insertion operation:

SELECT
*
FROM
tmp;

How to reset auto increment values in MySQL

We have three rows with column ID values: 1, 2 and 3. Perfect! It’s time to practice resetting the auto-increment value of the ID column.

Using ALTER TABLE instruction

You can reset the auto-increment value with the ALTER TABLE operator. The syntax of the ALTER TABLE operator to reset the autoincrement is as follows:

ALTER TABLE table_name AUTO_INCREMENT = value;

You specify the name of the table after ALTER TABLE and the value you want to reset in the expression AUTO_INCREMENT=value.

Note that the value must be greater than or equal to the current maximum value of the auto-increment column.

Let’s delete the last entry in the tmp table with id value 3:

DELETE FROM tmp
WHERE
ID = 3;

If you insert a new row, MySQL will assign 4 columns id of the new row. However, you can reset the number generated by MySQL to 3 using the following ALTER TABLE instruction:

ALTER TABLE tmp AUTO_INCREMENT = 3;

Now let’s try to insert a new line in the tmp table and request data from it to see the effect:

INSERT INTO tmp(name)
VALUES ('MySQL example 3');

SELECT
*
FROM
tmp;

How to reset auto increment values in MySQL

We have three lines with the last autoincrement value of 3 instead of 4, which is what we expected.

Using TRUNCATE TABLE operator

The TRUNCATE TABLE operator deletes all data from the table and resets the auto-increment value to zero.

The following illustrates the syntax of the TRUNCATE TABLE operator:

TRUNCATE TABLE table_name;

Using the TRUNCATE TABLE operator, you remove all data from the table forever and reset the auto-increment value to zero.

Using DROP TABLE and CREATE TABLE operators

You can use a pair of operators: DROP TABLE and CREATE TABLE to reset the auto-increment column. Note that this method removes all data from the table forever.

Like the TRUNCATE TABLE operator, these operators delete the table and recreate it, so the autoincrement value is reset to zero.

DROP TABLE table_name;
CREATE TABLE table_name(...);

In this article, you have learned how to reset the autoincrement value in MySQL in various ways. The first method is preferable because it is the simplest and has no side effects.

How To Reset MySQL Auto Increment Column – MySQL Developer Tutorial

Enteros

About Enteros

Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of RDBMS, NoSQL, and machine learning database platforms.

 
Tags: ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...