List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:April 8 2002 1:22am
Subject:Re: last_insert_id gives 0
View as plain text  
>On Sun, 7 Apr 2002 19:56:47 -0500
>Paul DuBois <paul@stripped> wrote:
>
>>  >I am running MySQL 3.23.41-17 on a stock SuSE 7.3 install.  I have a table
>>  >"customers" with an auto-incrementing primary key "customer_id". 
>>If I insert
>>  >a record:
>>  >mysql> insert into customers(customer_id) values ("10");
>>  >and then ask for the last insert:
>>  >mysql> select last_insert_id();
>>  >(which I thought was the correct syntax) I get the response "0".
>>
>>  You're inserting a specific value into the AUTO_INCREMENT field, which
>>  doesn't result in the creation of a new automatic sequence number.
>
>why don't just use "insert into customers(customer_id) values ("");" ???

Because he apparently wasn't trying to create an automatically generated
sequence number, he was trying insert a specific value and have it be
treated like an automatically generated sequence number.


By the way, it's better to insert NULL in your example than to insert
an empty string; your example relies implicitly on a convert-empty-string-
to-zero operation and on the behavior that inserting zero is currently
that same as inserting NULL.

>
>--
>Let's call it an accidental feature.
>	-- Larry Wall
>

Thread
last_insert_id gives 0Kevin Donnelly7 Apr
  • Re: last_insert_id gives 0Paul DuBois8 Apr
    • Re: last_insert_id gives 0Dicky Wahyu Purnomo8 Apr
      • Re: last_insert_id gives 0Paul DuBois8 Apr
    • Re: last_insert_id gives 0Kevin Donnelly8 Apr