- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to reset the primary key of a table in mysql?
The reset the primary key of a table means to reset the auto_increment property to 1. The syntax is as follows to reset the primary key of a table.
alter table yourTableName auto_increment = 1;
To understand, let us create a table −
mysql> create table ResetPrimaryKey −> ( −> Id int auto_increment, −> PRIMARY KEY(Id) −> ); Query OK, 0 rows affected (0.59 sec)
Insert some records into the table. The query to insert records is as follows −
mysql> insert into ResetPrimaryKey values(); Query OK, 1 row affected (0.18 sec) mysql> insert into ResetPrimaryKey values(); Query OK, 1 row affected (0.15 sec) mysql> insert into ResetPrimaryKey values(); Query OK, 1 row affected (0.09 sec) mysql> insert into ResetPrimaryKey values(); Query OK, 1 row affected (0.09 sec)
Now you can display all records with the help of select statement. The query is as follows −
mysql> select *from ResetPrimaryKey;
The following is the output that displays only the ID, which is a Primary Key:
+----+ | Id | +----+ | 1 | | 2 | | 3 | | 4 | +----+ 4 rows in set (0.00 sec)
Here is the query to reset the primary key of a table using alter −
mysql> alter table ResetPrimaryKey auto_increment = 1; Query OK, 0 rows affected (0.21 sec) Records: 0 Duplicates: 0 Warnings: 0
The query to check that we have added auto_increment property successfully or not:
mysql> desc ResetPrimaryKey;
The following is the output −
+-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | +-------+---------+------+-----+---------+----------------+ 1 row in set (0.11 sec)
- Related Articles
- Reset Primary Key in MySQL
- How to get primary key of a table in MySQL?
- Reset the primary key to 1 after deleting all the data in MySQL?
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- How to get the primary key “column name” of a specific table in MySQL?
- How to make MySQL table primary key auto increment?
- ALTER TABLE to add a composite primary key in MySQL?
- How to identify composite primary key in any MySQL database table?
- How to refer primary key as Foreign to various table in MySQL?
- How to find the primary key of a DB2 table TAB1?
- Can we remove a primary key from MySQL table?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- How to update DB2 table with a duplicate primary key?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?

Advertisements