
- 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 to auto increment with 1 after deleting data from a MySQL table?
For this, you can use TRUNCATE TABLE command. Let us first create a table −
mysql> create table DemoTable1796 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1796(StudentName) values('Chris Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1796(StudentName) values('David Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1796(StudentName) values('John Doe'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement
mysql> select * from DemoTable1796;
This will produce the following output
+-----------+--------------+ | StudentId | StudentName | +-----------+--------------+ | 1 | Chris Brown | | 2 | David Miller | | 3 | John Doe | +-----------+--------------+ 3 rows in set (0.00 sec)
Here is the query to delete records from the table −
mysql> delete from DemoTable1796; Query OK, 3 rows affected (0.00 sec)
Now insert a record into the table
mysql> insert into DemoTable1796(StudentName) values('John Doe'); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1796; +-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 4 | John Doe | +-----------+-------------+ 1 row in set (0.00 sec)
Now you can use TRUNCATE TABLE command
mysql> truncate table DemoTable1796; Query OK, 0 rows affected (0.00 sec)
Now, let us insert some records into the table
mysql> insert into DemoTable1796(StudentName) values('John Doe'); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1796; +-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | John Doe | +-----------+-------------+ 1 row in set (0.00 sec)
- Related Articles
- How to create a table with auto-increment column in MySQL using JDBC?
- How to make MySQL table primary key auto increment?
- Reorder keys after deleting a record from MySQL table?
- How to optimize a MySQL table after deleting some rows?
- How can I set my auto-increment value to begin from 1 in MySQL?
- Truncate a MySQL table and then set a custom value to auto increment
- Reset the primary key to 1 after deleting all the data in MySQL?
- How to change auto increment number in MySQL?
- How do I begin auto increment from a specific point in MySQL?
- Set custom Auto Increment with ZEROFILL in MySQL
- Set auto increment initial value for MySQL table using ALTER command
- How to Auto-Increment Cell Value in Excel after Each Printing?
- Passing NULL to MySQL for auto increment?
- Set MySQL int column to auto increment by 1 beginning at 10000?
- Deleting partial data from a field in MySQL?

Advertisements