MySQL query to get the next number in sequence for AUTO_INCREMENT field?


Let us first create a table −

mysql> create table DemoTable
-> (
-> Id int NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY(Id)
-> );
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.19 sec)

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

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

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

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

Following is the query to get the next number in sequence for AUTO_INCREMENT field −

mysql> SELECT auto_increment FROM information_schema.tables WHERE table_name='DemoTable';

Output

This will produce the following output −

+----------------+
| AUTO_INCREMENT |
+----------------+
| 4              |
+----------------+
1 row in set (0.26 sec)

Updated on: 30-Jun-2020

904 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements