
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reset AUTO_INCREMENT in MySQL
Truncate the table to reset AUTO_INCREMENT −
truncate table yourTableName;
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.52 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+ | Id | +----+ | 1 | | 2 | | 3 | | 4 | +----+ 4 rows in set (0.00 sec)
Following is the query to reset AUTO_INCREMENT −
mysql> truncate table DemoTable; Query OK, 0 rows affected (0.80 sec) Let us insert some records once again. It will by default begin from 1 for AUTO_INCREMENT: mysql> insert into DemoTable values(); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec)
Display all records from the table −
mysql> select *from DemoTable;
Output
+----+ | Id | +----+ | 1 | | 2 | +----+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL AUTO_INCREMENT with Examples
- Reset Primary Key in MySQL
- Working with AUTO_INCREMENT Columns in MySQL
- How to order by auto_increment in MySQL?
- Avoiding rewrite attributes with MySQL AUTO_INCREMENT
- Reset MySQL field to default value?
- AUTO_INCREMENT in MySQL can be signed by default?
- Alter a MySQL column to be AUTO_INCREMENT?
- How to reset auto-incrementing column in MySQL?
- What is the limit of auto_increment (integer) in MySQL?
- How can we change MySQL AUTO_INCREMENT starting number?
- Set AUTO_INCREMENT in a table while creating it in MySQL?
- In MySQL, what is the difference between SERIAL and AUTO_INCREMENT?
- How to insert own values into auto_increment column in MySQL?
- How to change a primary key in MySQL to auto_increment?
Advertisements