Afficher un message
Vieux 24/08/2007, 16h36   #5
Michael Dykman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: what if i need the primary key of a row that is to be inserted?

On 8/24/07, hardc0d3r <hardc0d3r@gmail.com> 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@gmail.com

- All models are wrong. Some models are useful.
  Réponse avec citation
 
Page generated in 0,04294 seconds with 9 queries