
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 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 Questions & Answers
- Reorder keys after deleting a record from MySQL table?
- How to make MySQL table primary key auto increment?
- How to create a table with auto-increment column in MySQL using JDBC?
- How to optimize a MySQL table after deleting some rows?
- How can I set my auto-increment value to begin from 1 in MySQL?
- Reset the primary key to 1 after deleting all the data in MySQL?
- Truncate a MySQL table and then set a custom value to auto increment
- Set MySQL int column to auto increment by 1 beginning at 10000?
- How to change auto increment number in MySQL?
- Set custom Auto Increment with ZEROFILL in MySQL
- How do I begin auto increment from a specific point in MySQL?
- Passing NULL to MySQL for auto increment?
- Set auto increment initial value for MySQL table using ALTER command
- Deleting partial data from a field in MySQL?
- Change the Auto Increment counter in MySQL?
Advertisements