Truncate a MySQL table and then set a custom value to auto increment

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable825(Value int NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.63 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable825 values();
Query OK, 1 row affected (0.16 sec)  
mysql> insert into DemoTable825 values();
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable825 values();
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable825;

This will produce the following output −

+-------+
| Value |
+-------+
|     1 |
|     2 |
|     3 |
+-------+
3 rows in set (0.00 sec)

Following is the query to truncate the table and then update auto_increment value −

mysql> truncate table DemoTable825;
Query OK, 0 rows affected (0.58 sec)

Following is the query to change the auto_increment value −

mysql> alter table DemoTable825 auto_increment=2000;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

Insert some records in the table using insert command −

mysql> insert into DemoTable825 values();
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable825 values();
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable825 values();
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable825;

This will produce the following output −

+-------+
| Value |
+-------+
| 2000  |
| 2001  |
| 2002  |
+-------+
3 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 12:13:46
Advertisements