List:General Discussion« Previous MessageNext Message »
From:Michael Dykman Date:August 24 2007 3:36pm
Subject:Re: what if i need the primary key of a row that is to be inserted?
View as plain text  
On 8/24/07, hardc0d3r <hardc0d3r@stripped> wrote:
>
> yup, that's it.. thanks for the reply..
> --


More typically, you would use the last_insert_id() function which will
return the value of the most recently created auto-increment key...

CREATE TABLE mytable (
id int auto_increment primary key,
foo char(1)
);

 INSERT INTO mytable (foo) VALUES('a');
SELECT LAST_INSERT() AS newkey;

calculating the max key + 1 , as it has been noted, must be done
transactionally and will therefore require a transactional table
type...  The catch is that, unlike MyISAM, Innodb does not maintain
column meta information like min, max, sum so this approach will get
more expensive with each subsequent call.
-- 
 - michael dykman
 - mdykman@stripped

 - All models are wrong.  Some models are useful.
Thread
what if i need the primary key of a row that is to be inserted?hardc0d3r24 Aug
  • Re: what if i need the primary key of a row that is to be inserted?Martijn Tonies24 Aug
    • Re: what if i need the primary key of a row that is to be inserted?hardc0d3r24 Aug
      • Re: what if i need the primary key of a row that is to be inserted?Michael Dykman24 Aug
        • Re: what if i need the primary key of a row that is to be inserted?Rudy Lippan24 Aug
  • Re: what if i need the primary key of a row that is to be inserted?Martijn Tonies24 Aug