Mysql auto increment without insert

I have a mysql table with an auto-increment field. I use the auto increment counter as an identifier generator across the entire database.

I need a way to increment the counter, get a new number, but not insert any data.

Is it easy to do?

+3
source share
3 answers

If you use it as an identifier generator, then (in addition to modifying the table) you will need to insert something to get the next value.

The easiest way to do this without inserting any data is to do INSERTand then call LAST_INSERT_IDand then release ROLLBACK. This will cause the insert to roll back, but the next identifier will still be incremented.

+2

,

SHOW TABLE STATUS LIKE 'table_name'

Auto_increment ,

PHP

$result = mysql_query("SHOW TABLE STATUS LIKE 'table_name'");
$row = mysql_fetch_assoc($result);
$nextAutoIncrement = $row['Auto_increment'];
0

, ,

ALTER TABLE table_name AUTO_INCREMENT = id+1

here id will be your auto-increment field. But Auto-increment fields increase relative to the maximum value in this column.

-1
source

All Articles