Can I find out the next auto_increment to be used?


Yes, you can find out the next auto_increment with SELECT AUTO_INCREMENT as shown in the below syntax −

SELECT AUTO_INCREMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA= yourDatabaseName AND TABLE_NAME=yourTableName;

Let us first create a table −

mysql> create table DemoTable
(
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(20),
   ClientAge int
);
Query OK, 0 rows affected (1.33 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(ClientName,ClientAge) values('John',23);
Query OK, 1 row affected (0.35 sec)
mysql> insert into DemoTable(ClientName,ClientAge) values('Carol',21);
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable(ClientName,ClientAge) values('Bob',24);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(ClientName,ClientAge) values('David',25);
Query OK, 1 row affected (0.25 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
| 1        | John       | 23        |
| 2        | Carol      | 21        |
| 3        | Bob        | 24        |
| 4        | David      | 25        |
+----------+------------+-----------+
4 rows in set (0.00 sec)

Here is the query to find out the next auto_increment to be used in the above table.

mysql> SELECT AUTO_INCREMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA= 'sample' AND TABLE_NAME='DemoTable';

This will produce the following output −

+----------------+
| AUTO_INCREMENT |
+----------------+
| 5              |
+----------------+
1 row in set (0.20 sec)

Updated on: 30-Jul-2019

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements