Perl MySQL NEXT_INSERT_ID?

E

Eric

Hi,

I know I can get LAST_INSERT_ID from a MySQL database... but is there a way
to determine what the next INSERT_ID would be if I were to insert a new
record?

Thanks,

Eric
 
G

Gregory Toomey

Eric said:
Hi,

I know I can get LAST_INSERT_ID from a MySQL database... but is there a
way to determine what the next INSERT_ID would be if I were to insert a
new record?

Thanks,

Eric

Select max(key)+1 as nextkey from table

should work.

gtoomey
 
E

Eric

I know I can get LAST_INSERT_ID from a MySQL database... but is there a
Select max(key)+1 as nextkey from table

Surely that will only work if there's not been any record deletions since
the last inserion?

Eric
 
A

Andy Hassall

Not reliably, no. You can only get the ID after you insert the record.
http://www.mysql.com/doc/en/SHOW_TABLE_STATUS.html

Run the query SHOW TABLE STATUS on your table, and find the value of the
column "auto_increment". This will give you the next auto-incrementing id
value.

... at the time of the call.

You then have a race condition if you rely on that value, since there's
nothing stopping it being taken by another session in the meantime.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top