Tutorialspoint

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


1 Answer
karthikeya Boyini

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;

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';

This will produce the following output −

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.