
- 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
Reset the primary key to 1 after deleting all the data in MySQL?
To reset the primary key to 1 after deleting the data, use the following syntax
alter table yourTableName AUTO_INCREMENT=1; truncate table yourTableName;
After doing the above two steps, you will get the primary key beginning from 1.
To understand the above concept, let us create a table. The query to create a table is as follows
mysql> create table resettingPrimaryKeyDemo -> ( -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY -> ); Query OK, 0 rows affected (0.66 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into resettingPrimaryKeyDemo values(); Query OK, 1 row affected (0.15 sec) mysql> insert into resettingPrimaryKeyDemo values(); Query OK, 1 row affected (0.10 sec) mysql> insert into resettingPrimaryKeyDemo values(); Query OK, 1 row affected (0.08 sec) mysql> insert into resettingPrimaryKeyDemo values(); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from resettingPrimaryKeyDemo;
The following is the output
+--------+ | UserId | +--------+ | 1 | | 2 | | 3 | | 4 | +--------+ 4 rows in set (0.00 sec)
Here is the query to reset the primary key to 1
mysql> alter table resettingPrimaryKeyDemo AUTO_INCREMENT=1; Query OK, 0 rows affected (0.14 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> truncate table resettingPrimaryKeyDemo; Query OK, 0 rows affected (0.89 sec)
Check the records from the table. The query is as follows −
mysql> select *from resettingPrimaryKeyDemo; Empty set (0.00 sec)
Insert some records from the table using insert command. The query is as follows −
mysql> insert into resettingPrimaryKeyDemo values(); Query OK, 1 row affected (0.12 sec) mysql> insert into resettingPrimaryKeyDemo values(); Query OK, 1 row affected (0.10 sec) mysql> insert into resettingPrimaryKeyDemo values(); Query OK, 1 row affected (0.10 sec)
Now check the table primary key beginning from 1. The query is as follows −
mysql> select *from resettingPrimaryKeyDemo;
The following is the output
+--------+ | UserId | +--------+ | 1 | | 2 | | 3 | +--------+ 3 rows in set (0.00 sec)
- Related Articles
- Reset Primary Key in MySQL
- How to reset the primary key of a table in mysql?
- How to auto increment with 1 after deleting data from a MySQL table?
- How to insert data to MySQL having auto incremented primary key?
- Remove Primary Key in MySQL?
- Is the primary key automatically indexed in MySQL?
- Deleting all license key in SAP HANA
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- Set existing column as Primary Key in MySQL?
- ALTER TABLE to add a composite primary key in MySQL?
- How to get primary key of a table in MySQL?
- How to make MySQL table primary key auto increment?
- MySQL ALTER column to remove primary key and auto_increment?
- How to change a primary key in MySQL to auto_increment?\n
- Classify the data in Q.1 above as primary or secondary data.

Advertisements