Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current AUTO_INCREMENT value for any table

How do I get the current AUTO_INCREMENT value for a table in MySQL?

like image 926
bparise Avatar asked Apr 04 '13 20:04

bparise


People also ask

How can I get auto increment value?

The starting value for AUTO_INCREMENT is 1, which is the default. It will get increment by 1 for each new record. To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT. Creating a table, with “id” as auto-increment.

How can get last auto increment value in SQL?

To obtain the value immediately after an INSERT , use a SELECT query with the LAST_INSERT_ID() function. For example, using Connector/ODBC you would execute two separate statements, the INSERT statement and the SELECT query to obtain the auto-increment value.

How do I get next available ID in SQL?

To do this, use a transaction in which you execute the insert and then query for the id like: INSERT INTO table (col1) VALUES ("Text"); SELECT LAST_INSERT_ID(); The returnset now contains only one column which holds the id of the newly generated row.

What is AUTO_INCREMENT in SQL?

The auto increment in SQL is a feature that is applied to a field so that it can automatically generate and provide a unique value to every record that you enter into an SQL table. This field is often used as the PRIMARY KEY column, where you need to provide a unique value for every record you add.


2 Answers

You can get all of the table data by using this query:

SHOW TABLE STATUS FROM `DatabaseName` WHERE `name` LIKE 'TableName' ; 

You can get exactly this information by using this query:

SELECT `AUTO_INCREMENT` FROM  INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'DatabaseName' AND   TABLE_NAME   = 'TableName'; 
like image 147
methai Avatar answered Sep 28 '22 13:09

methai


If you just want to know the number, rather than get it in a query then you can use:

SHOW CREATE TABLE tablename; 

You should see the auto_increment at the bottom

like image 41
johnnyjohnny Avatar answered Sep 28 '22 13:09

johnnyjohnny