tags:

views:

61

answers:

2

I've got an application in php & mysql where the users writes and reads from a particular table. One of the write modes is in a batch, doing only one query with the multiple values. The table has an ID which auto-increments. The idea is that for each row in the table that is inserted, a copy is inserted in a separate table, as a history log, including the ID that was generated.

The problem is that multiple users can do this at once, and I need to be sure that the ID loaded is the correct.

Can I be sure that if I do for example:

INSERT INTO table1 VALUES ('','test1'),('','test2')

that the ids generated are sequential?

How can I get the Id's that were just loaded, and be sure that those are the ones that were just loaded?

I've thinked of the LOCK TABLE, but the users shouldn't note this.

Hope I made myself clear...

A: 

based on the current implementation of myisam and innodb, yes. however, this is not guaranteed to be so in the future, so i would not rely on it.

longneck
+1  A: 

Building an application that requires generated IDs to be sequential usually means you're taking a wrong approach - what happens when you have to delete a value some day, are you going to re-sequence the entire table? Much better to just let the values fall as they may, using a primary key to prevent duplication.

TML
Just this ID field is unique, all the others can be repeated.And I'll never delete any value from it. It's just for store things and take a few reports from there.
nightshadex101
OK - but it's still a requirement that usually doesn't make sense. One of the great features of an RDBMS is that you don't have to concern yourself with things like this.Is there a particular reason you want them to be sequential?
TML
I just want to be able to do an>INSERT INTO table1_hist VALUES ('<id1>','test1'),('<id1>','test2')but whit hundreds of values at a time. I can do it one by one, but I don't think the performance will be the same.And this will be done by hundreds of users at the same time, I must be sure that if I load 5 rows, I'll get those 5 ids (whatever they are) and I'll be able to load them in the historical table.
nightshadex101
The performance of `INSERT INTO table1_hist VALUES ('<id1>','test1'),('<id1>','test2'), ..., ('<id1>', 'test<n>')` wouldn't be any different than the performance of `INSERT INTO table1_hist VALUES ('<id1>','test1'); INSERT INTO table1_hist VALUES ('<id1>','test2'); ...; INSERT INTO table1_hist VALUES ('<id1>','test<n>');`As for "loading them into the historical table", it sounds like what you should REALLY be looking at is at leastin using a transaction, and perhaps even going to a stored procedure...
TML