Getting the next primary key without adding a new record is impossible, isn't it?

时间:2015-07-28 15:40:26

标签: php mysql sql

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.

3 个答案:

答案 0 :(得分:2)

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"

答案 1 :(得分:2)

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.

答案 2 :(得分:0)

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.