Hi Sean,

> "If expr is given as an arguement to LAST_INSERT_ID(), the value of the
> arguement is returned by the function, is set as the next value to be
> returned by LAST_INSERT_ID(), and is used as the next AUTO_INCREMENT
> value...."

I don't think this entire statement is correct. The online docs don't have
the last part of this sentence in their explanation in section 6.3. While
the first two statements are true, last_insert_id(expr) does not set the
next auto_increment value for any table.

>From my experience, it's better to think of last_insert_id() as a buffer
that holds the last auto_increment value on the current connection. If you
supply a value to the function, i.e. last_insert_id(1000), you're just
storing the value in the buffer for later retrieval. You're not affecting
the auto_increment value for any table.

The explanation in Paul DuBois' book MySQL is a little clearer:

"With an argument, last_insert_id() is intended to be used in an UPDATE
statement. The result is treated the same way as an automatically
generated value, which is useful for generating sequences."

So, the typical use of last_insert_id(expr) is for generating sequences
without using an auto_increment column. Again, from Paul's book:

CREATE TABLE seq_table ( seq INT UNSIGNED NOT NULL );
INSERT INTO seq_table VALUES (0);

To generate sequences using this table:

UPDATE seq_table SET seq = LAST_INSERT_ID(seq + 1);

To retrieve the sequence number just generated, now call last_insert_id()
without an argument. As you can see, you're not affecting the current
auto_increment value of any table. You're just storing the value of the
expression for later retrieval. So, SELECT last_insert_id(1000) does
nothing but store the value 1000 in the last_insert_id() buffer -- which
means the next time you call last_insert_id() without an expression, it
will return the value 1000.

Hope this helps.

--jeff







---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to