I have a problem with LAST_INSERT_ID() (or mysql_insert_id on php) and a table with two parameters (for this example).
ID: autoincremental value
TEXT: varchar unique field.
If I have this:
INSERT INTO field (text) VALUES ('a'); -> Insert OK
SELECT LAST_INSERT_ID(); -> ok, returns 1;
INSERT INTO field (text) VALUES ('a'); -> Insert Error (text field is UNIQUE)
INSERT INTO field (text) VALUES ('b'); -> Insert Ok
SELECT LAST_INSERT_ID(); -> ok, but returns 3, not 2
The autoincremental field isn’t correlative, and jump the 2 value.
I try whith START TRANSACTION and ROLLBACK or COMMIT, but I have the same problem (or maybe, I don’t do it correctly).
Any idea?
Thanks.
No, an incremental ID isn’t always MAX+1, nor do they reshuffle if you delete an earlier record in sequence. It shouldn’t matter if you have gaps in the sequence of your incremental IDs: if you’re using incremental IDs correctly as unique identifiers, then this is NOT A PROBLEM. If you’re dependent on a consecutive sequence, then you haven’t designed your database correctly.