149

How to get the next id in mysql to insert it in the table

INSERT INTO payments (date, item, method, payment_code)
VALUES (NOW(), '1 Month', 'paypal', CONCAT("sahf4d2fdd45", id))
7
  • 1
    use an auto_increment column
    – knittl
    Jul 20, 2011 at 11:55
  • Do you want the next id, or the id of the row that you are currently inserting? That is, should the id at the end of the payment code be the id of the row in which the payment code is stored?
    – Mike
    Jul 20, 2011 at 12:30
  • id of the row that I am currently inserting
    – faressoft
    Jul 20, 2011 at 12:49
  • 7
    In that case, concatenate the values when you retrieve them, not when you insert them. It's far easier that way, and you rule out getting the wrong id, or having to run an update - think what would happen if the row you have just inserted is requested by another client, before the update has had chance to run: the client would end up with an invalid payment code. On a low-traffic system, that might not occur, but I don't see the point of taking the risk.
    – Mike
    Jul 20, 2011 at 12:53
  • ...or as @binaryLV points out, resource locking might also solve the problem. See: dev.mysql.com/doc/refman/5.5/en/lock-tables.html
    – Mike
    Jul 20, 2011 at 13:05

21 Answers 21

295

You can use

SET information_schema_stats_expiry = 0;
SELECT AUTO_INCREMENT
FROM information_schema.tables
WHERE table_name = 'table_name'
AND table_schema = DATABASE( );

The default information_schema_stats_expiry value is 86400 so without the first statement (only needed once per session) this method can return stale data.

Alternatively, you can use this query and the value will be in the Auto_increment column but other additional data is also returned:

SHOW TABLE STATUS LIKE 'table_name';
12
  • 1
    @GerardONeill Removed it
    – ravi404
    Feb 15, 2016 at 7:05
  • 7
    because a cache is used to retrieve the data from information_schema.tables, this solution do no more work for mysql 8.
    – Bruno
    Feb 6, 2019 at 16:39
  • 1
    @Bruno can you post a reference?
    – mvorisek
    Sep 2, 2019 at 19:24
  • 3
    This official doc state that TABLES.AUTO_INCREMENT is cached dev.mysql.com/doc/refman/8.0/en/… . Mysql blog also has several post about it, but the system varaible they mention seems outdated: mysqlserverteam.com/… mysqlserverteam.com/…
    – Bruno
    Sep 3, 2019 at 13:35
  • 3
    the second query does return the next auto increment value, but it is cached (default cache duration is 24h). To get the UP TO DATE auto increment value just run SET information_schema_stats_expiry = 0; before the query.
    – aetonsi
    May 12, 2022 at 7:57
57

You can get the next auto-increment value by doing:

SHOW TABLE STATUS FROM tablename LIKE Auto_increment
/*or*/
SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES
WHERE table_name = 'tablename'

Note that you should not use this to alter the table, use an auto_increment column to do that automatically instead.
The problem is that last_insert_id() is retrospective and can thus be guaranteed within the current connection.
This baby is prospective and is therefore not unique per connection and cannot be relied upon.
Only in a single connection database would it work, but single connection databases today have a habit of becoming multiple connection databases tomorrow.

See: SHOW TABLE STATUS

13
  • 10
    I didn't downvote it, but the problem with attempting to use the last auto incrementing value is that it might not be the last one by the time you come to use it - no matter how quickly the SELECT and subsequent INSERT is carried out.
    – Mike
    Jul 20, 2011 at 12:33
  • 1
    @binaryLV: The smaller the gap between the SELECT and the INSERT, the smaller the chance that the value will have changed, but it doesn't rule it out completely. Imagine a system with thousands of hits per minute on the database - the chances of the value having changed increase dramatically. Table or row locking may prevent this if it is done as a single query, but that's relying on a particular behaviour of the database engine which may not be well documented. I'd go with a subsequent UPDATE if I had too. But I'd rather just concatenate the two at display time, and save all the hassle.
    – Mike
    Jul 20, 2011 at 12:44
  • 1
    basically need to see what the next autoincrement will be. This is the best option because I haven't inserted anything yet! Just be sure to lock the table where the records will be inserted BEFORE calling this and using it later (after you unlock of course!) ex. LOCK TABLES t1 READ;
    – Bretticus
    Aug 8, 2013 at 22:32
  • 7
    SHOW TABLE STATUS expects to see database name after FROM and 'table name pattern' after LIKE.
    – x-yuri
    Dec 8, 2014 at 13:49
  • 4
    because a cache is used to retrieve the data from information_schema.tables, this solution do no more work for mysql 8.
    – Bruno
    Feb 6, 2019 at 16:40
21

This will return auto increment value for the MySQL database and I didn't check with other databases. Please note that if you are using any other database, the query syntax may be different.

SELECT AUTO_INCREMENT 
FROM information_schema.tables
WHERE table_name = 'your_table_name'
     and table_schema = 'your_database_name';

SELECT AUTO_INCREMENT 
FROM information_schema.tables
WHERE table_name = 'your_table_name'
     and table_schema = database();
1
  • 1
    SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = 'your_table_name' and table_schema = 'your_database_name';
    – LJay
    Jan 17, 2020 at 11:10
11

The top answer uses PHP MySQL_ for a solution, thought I would share an updated PHP MySQLi_ solution for achieving this. There is no error output in this exmaple!

$db = new mysqli('localhost', 'user', 'pass', 'database');
$sql = "SHOW TABLE STATUS LIKE 'table'";
$result=$db->query($sql);
$row = $result->fetch_assoc();

echo $row['Auto_increment'];

Kicks out the next Auto increment coming up in a table.

1
  • because a cache is used to retrieve the data from information_schema.tables, this solution do no more work for mysql 8.
    – Bruno
    Feb 6, 2019 at 16:40
10

In PHP you can try this:

$query = mysql_query("SELECT MAX(id) FROM `your_table_name`");
$results = mysql_fetch_array($query);
$cur_auto_id = $results['MAX(id)'] + 1;

OR

$result = mysql_query("SHOW TABLE STATUS WHERE `Name` = 'your_table_name'");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];
6
  • 5
    Bear in mind with the first method if the record with the highest id is dropped, you will then create a new record with the id the dropped one used to have. Jan 31, 2015 at 15:31
  • and if the previous key was used in another tables and is still kept there you can end up with very strange magic
    – Tebe
    Aug 21, 2015 at 5:34
  • 2
    the first one return wrong id if you drop last inserted record,
    – Ali Parsa
    Feb 9, 2016 at 16:17
  • If records are dropped from the table, then it will return the wrong id. Nov 28, 2022 at 18:21
  • @Tom Jenkinson, not so sure of this. In fact if you delete al the rows in the table the auto-increment continues from the previous value? I am recreating a table to renumber thiem.
    – mckenzm
    Jan 1, 2023 at 9:52
7

Use LAST_INSERT_ID() from your SQL query.

Or

You can also use mysql_insert_id() to get it using PHP.

4
  • 33
    @Sarfraz: The question was about the NEXT id not the LAST as you said mentioning LAST_INSERT_ID(). Jan 19, 2017 at 14:44
  • 5
    If rows are deleted in the table the max will be wrong Feb 16, 2017 at 9:43
  • 1
    just a note from '17 - mysql* functions are deprecated - above can be done with mysqli_insert_id
    – treyBake
    Jun 28, 2017 at 8:35
  • 1
    who marked this as answer? the question is about next insert id not last.
    – Amit Shah
    Oct 24, 2018 at 10:36
7

Solution:

CREATE TRIGGER `IdTrigger` BEFORE INSERT ON `payments`
  FOR EACH ROW
BEGIN

SELECT  AUTO_INCREMENT Into @xId
    FROM information_schema.tables
    WHERE 
    Table_SCHEMA ="DataBaseName" AND
    table_name = "payments";

SET NEW.`payment_code` = CONCAT("sahf4d2fdd45",@xId);

END;

"DataBaseName" is the name of our Data Base

6

Simple query would do SHOW TABLE STATUS LIKE 'table_name'

6

For MySQL 8 use SHOW CREATE TABLE to retrieve the next autoincrement insert id:

SHOW CREATE TABLE mysql.time_zone

Result:

CREATE TABLE `time_zone` (
  `Time_zone_id` int unsigned NOT NULL AUTO_INCREMENT,
  `Use_leap_seconds` enum('Y','N') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Time_zone_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1784 DEFAULT CHARSET=utf8 STATS_PERSISTENT=0 ROW_FORMAT=DYNAMIC COMMENT='Time zones'

See the AUTO_INCREMENT=1784 at the last line of returned query.

Compare with the last value inserted: select max(Time_zone_id) from mysql.time_zone

Result:

+-------------------+
| max(Time_zone_id) |
+-------------------+
|              1783 |
+-------------------+

Tested on MySQL v8.0.20.

2
  • good to see the select max(), for a value that is likely indexed, and likely in buffer.
    – mckenzm
    Jan 1, 2023 at 9:47
  • 1
    It is wrong to use max() to get the next auto-increment id. I've put it here just for result comparison.
    – Alexeyer
    Jan 5, 2023 at 10:07
5
SELECT id FROM `table` ORDER BY id DESC LIMIT 1

Although I doubt in its productiveness but it's 100% reliable

4
  • I too agree this is the simplest approach. Not sure why you were down-voted, but I'll offset with an upvote.
    – recurse
    Jul 25, 2016 at 20:56
  • I didn't mention necessity of transaction, if you don't wrap it inside transaction this code is lousy asap it meets real loading.
    – Tebe
    Jul 25, 2016 at 21:03
  • 1
    What if the latest row was deleted? Or multiple latest rows have been deleted?
    – Liam W
    Oct 26, 2016 at 17:36
  • Non-issue, freed keys don't get used unless you specify it explicitly
    – Tebe
    Oct 26, 2016 at 19:21
4

You have to connect to MySQL and select a database before you can do this

$table_name = "myTable"; 
$query = mysql_query("SHOW TABLE STATUS WHERE name='$table_name'"); 
$row = mysql_fetch_array($query); 
$next_inc_value = $row["AUTO_INCREMENT"];  
4

I suggest to rethink what you are doing. I never experienced one single use case where that special knowledge is required. The next id is a very special implementation detail and I wouldn't count on getting it is ACID safe.

Make one simple transaction which updates your inserted row with the last id:

BEGIN;

INSERT INTO payments (date, item, method)
     VALUES (NOW(), '1 Month', 'paypal');

UPDATE payments SET payment_code = CONCAT("sahf4d2fdd45", LAST_INSERT_ID())
     WHERE id = LAST_INSERT_ID();

COMMIT;
3
  • I can tell one such use-case, I am trying to diagnose a production issue, so I need to find out if the last row in a table is really the last row or if there was one added after that which somehow got deleted, table has an auto_increment field in it, so by finding this information I can conclude if the row was deleted or never added.
    – Usman
    Nov 1, 2018 at 19:00
  • 1
    That might maybe work for you, but I wouldn't rely on that as I don't think you have any guarantee on that: dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html "when the AUTO_INCREMENT column is part of a multiple-column index), AUTO_INCREMENT values are reused if you delete the row with the biggest AUTO_INCREMENT value in any group." Nov 17, 2018 at 21:52
  • Thanks, useful information, also as per your shared link auto_increment can be reset be manually inserting a number, so yes not reliable.
    – Usman
    Nov 19, 2018 at 23:10
3

You can't use the ID while inserting, neither do you need it. MySQL does not even know the ID when you are inserting that record. You could just save "sahf4d2fdd45" in the payment_code table and use id and payment_code later on.

If you really need your payment_code to have the ID in it then UPDATE the row after the insert to add the ID.

3
  • +1 To make this a little more explicit: if you grab the 'latest' value from a column, it is only guaranteed to be the latest value at the exact moment you grab it. It may not be the latest value when you come to use that value in a subsequent insert. In the case of an auto increment column, there's always the chance that another value has been added between the time that you retrieved the id and the time that you insert it elsewhere. If you are referencing a row that you have just inserted, using LAST_INSERT_ID() is fine. If you are trying to ensure a unique value, it is not.
    – Mike
    Jul 20, 2011 at 12:24
  • 1
    @cularis, MySQL does know the next auto_increment id, it's listed in the information_schema.tables table.
    – Johan
    Jul 20, 2011 at 12:27
  • 1
    @faressoft: If the idea is to have a payment code that comprises a unique string plus the id of the row that contains that payment code, just combine the two when you retrieve the row - either with a SELECT ... CONCAT(payment_code, id), or in your application code. You could even wrap the SELECT in a VIEW, so that you always return the right value, without worrying about the CONCAT in every SELECT from your app.
    – Mike
    Jul 20, 2011 at 12:27
3

What do you need the next incremental ID for?

MySQL only allows one auto-increment field per table and it must also be the primary key to guarantee uniqueness.

Note that when you get the next insert ID it may not be available when you use it since the value you have is only within the scope of that transaction. Therefore depending on the load on your database, that value may be already used by the time the next request comes in.

I would suggest that you review your design to ensure that you do not need to know which auto-increment value to assign next

1
  • It is not a good idea to assume the kind of application that requires the ID. There are applications like the one I am working on at the moment which needs the next incremental ID and the value retrieved is in no danger of being allocated to another script.
    – JG Estiot
    Jan 31, 2016 at 6:37
2

use "mysql_insert_id()". mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.

Below are the example of use:

<?php
    $link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable  VALUES('','value')");
printf("Last inserted record has id %d\n", mysql_insert_id());
    ?>

I hope above example is useful.

1
2

If return no correct AUTO_INCREMENT, try it:

ANALYZE TABLE `my_table`;
SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE (TABLE_NAME = 'my_table');

This clear cache for table, in BD

1
  • If you dont clear the cache it keeps grabbing the first one it found over and over, so this is good information . Sep 20, 2022 at 16:27
1

using the answer of ravi404:

CREATE FUNCTION `getAutoincrementalNextVal`(`TableName` VARCHAR(50))
    RETURNS BIGINT
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN

    DECLARE Value BIGINT;

    SELECT
        AUTO_INCREMENT INTO Value
    FROM
        information_schema.tables
    WHERE
        table_name = TableName AND
        table_schema = DATABASE();

    RETURN Value;

END

using in your insert query, to create a SHA1 Hash. ex.:

INSERT INTO
    document (Code, Title, Body)
VALUES (                
    sha1( getAutoincrementalNextval ('document') ),
    'Title',
    'Body'
);
1
  • Does this always work? Is there a race conditions if two inserts happen at the same time?
    – Jmons
    Oct 27, 2016 at 10:07
0

Improvement of @ravi404, in case your autoincrement offset IS NOT 1 :

SELECT (`auto_increment`-1) + IFNULL(@@auto_increment_offset,1) 
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name = your_table_name
AND table_schema = DATABASE( );

(auto_increment-1) : db engine seems to alwaus consider an offset of 1. So you need to ditch this assumption, then add the optional value of @@auto_increment_offset, or default to 1 : IFNULL(@@auto_increment_offset,1)

0

For me it works, and looks simple:

 $auto_inc_db = mysql_query("SELECT * FROM my_table_name  ORDER BY  id  ASC ");
 while($auto_inc_result = mysql_fetch_array($auto_inc_db))
 {
 $last_id = $auto_inc_result['id'];
 }
 $next_id = ($last_id+1);


 echo $next_id;//this is the new id, if auto increment is on
2
  • Quite unnecessary to assign $last_id repetitively when you can just DESC. Your while block did loads of useless work.
    – Til
    Jan 18, 2019 at 11:40
  • 1
    yes, right...I've just realized it's not perfect...:( end one can repeat the id, if it was deleted before... :(
    – Toncsiking
    Jan 18, 2019 at 11:44
0

SELECT AUTO_INCREMENT AS next_id FROM information_schema.tables WHERE table_name = 'table name' AND table_schema = 'database name of table name'

-1
mysql_insert_id();

That's it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.