
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I set my auto-increment value to begin from 1 in MySQL?
You can truncate the table to set auto_increment value to start from 1 in MySQL. Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY ); Query OK, 0 rows affected (1.44 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.06 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+ | StudentId | +-----------+ | 1 | | 2 | | 3 | | 4 | +-----------+ 4 rows in set (0.00 sec)
Now, if you will insert more values, then the StudentId, which is AUTO_INCREMENT will continue from 5. To avoid this, first, truncate the table −
Following is the query to truncate the table to start auto_increment value from 1 −
mysql> truncate table DemoTable; Query OK, 0 rows affected (0.93 sec) mysql> select *from DemoTable; Empty set (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output. Now, since we truncated the table above, therefore the AUTO_INCREMENT begins from 1 again as shown in the below output −
+-----------+ | StudentId | +-----------+ | 1 | | 2 | | 3 | +-----------+ 3 rows in set (0.00 sec)
- Related Articles
- How do I begin auto increment from a specific point in MySQL?
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- How to set initial value and auto increment in MySQL?
- What if I forgot to set Auto Increment? Can I set it later in MySQL?
- Display auto increment user id sequence number to begin from 001 in MySQL?
- How to auto-increment value of tables to lower value in MySQL?
- Set MySQL int column to auto increment by 1 beginning at 10000?
- Set auto increment initial value for MySQL table using ALTER command
- How to auto increment with 1 after deleting data from a MySQL table?
- Set custom Auto Increment with ZEROFILL in MySQL
- Truncate a MySQL table and then set a custom value to auto increment
- How to change auto increment number in MySQL?
- How can I calculate the total value of products from my MySQL product table?
- Autoincrement in MySQL begins from 1? How can we begin it from another number?
- Changing the current count of an Auto Increment value in MySQL?
