0

I have a table with a field 'Text'. The 'id' field is automatically increased when data is inserted. I run this query:

INSERT into answer (Text) VALUES ('Test');
INSERT into answer (Text) VALUES ('Test');
INSERT into answer (Text) VALUES ('Test');
INSERT into answer (Text) VALUES ('Test');
Delete From answer;

I need get future id (when I will insert next text). Can you help me with query, that take me number = 5 (but not 1, because 'id' automatically increased)?

iCantSeeSharp
  • 3,880
  • 4
  • 42
  • 65
titans
  • 421
  • 3
  • 5
  • 13

1 Answers1

4

You cannot know the next auto_increment value. Well, probably not the way you think of it. You can only reliably obtain the last one used (by using LAST_INSERT_ID()).

Some would probably tell you the other way: "this is easy, just take LAST_INSERT_ID()+1".

But this is a bad idea. Why? Because there will be concurrent access to your database. So you probably could guess the next ID in the table. But not the next ID that will be attributed in your transaction.

(in addition, depending your DB setup, auto_increment step might be greater than 1 -- see http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_increment)

Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125