在不添加新记录的情况下获取下一个主键是不可能的,不是吗? [重复]

This question already has an answer here:

I considered the top google results, almost all functions of mysql and etc... Unfortunately, as far as I learned, without adding a new row into the table of a database, we can't know the next primary key. Please, say me I'm wrong. How can't there be any solution for this problem? I am disappointed.

</div>

Took about 7 seconds to find on Google: https://www.bram.us/2008/07/30/mysql-get-next-auto_increment-value-fromfor-table/

SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "databaseName"
AND TABLE_NAME = "tableName"

The value is present in INFORMATION_SCHEMA.TABLES (see here). Under reasonable assumptions, you can get the value there.

Reasonable assumptions:

  • No changes to the system variables that affect auto increment.
  • No concurrent transactions.
  • No intervening reset of the value.

with this request you have the last id in your table

SELECT MAX(id) FROM your_table

You can just add +1 to your request result.