
- 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 Primary Key in MySQL
To reset primary key, at first use TRUNCATE table, then use ALTER TABLE. Let us first create a table −
mysql> create table DemoTable1929 ( UserId int NOT NULL AUTO_INCREMENT, PRIMARY KEY(UserId) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1929 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1929 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1929 values(); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1929;
This will produce the following output −
+--------+ | UserId | +--------+ | 1 | | 2 | | 3 | +--------+ 3 rows in set (0.00 sec)
Here is the query to reset primary key −
mysql> truncate table DemoTable1929; Query OK, 0 rows affected (0.00 sec) mysql> alter table DemoTable1929 auto_increment=1; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
Now you can insert record, it will reset the primary key value from 1 −
mysql> insert into DemoTable1929 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1929 values(); Query OK, 1 row affected (0.00 sec)
Display all records from table using select statement −
mysql> select * from DemoTable1929;
This will produce the following output −
+--------+ | UserId | +--------+ | 1 | | 2 | +--------+ 2 rows in set (0.00 sec)
- Related Articles
- How to reset the primary key of a table in mysql?
- Reset the primary key to 1 after deleting all the data in MySQL?
- Remove Primary Key in MySQL?
- Set existing column as Primary Key in MySQL?
- Is the primary key automatically indexed in MySQL?
- How do I drop a primary key in MySQL?
- Primary key Vs Unique key
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- Two columns as primary key with auto-increment 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?
- Can we remove a primary key from MySQL table?
- How to change a primary key in MySQL to auto_increment?\n

Advertisements