PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > what if i need the primary key of a row that is to be inserted?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
what if i need the primary key of a row that is to be inserted?

Réponse
 
LinkBack Outils de la discussion
Vieux 24/08/2007, 15h25   #1 (permalink)
hardc0d3r
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut what if i need the primary key of a row that is to be inserted?


what if i need the primary key of a row that is to be inserted? that primary
key will be used to insert another row from another table.. what i was doing
was after inserting the row, i get the primary key by select statement and
use that value to insert a row from another table.. is this ok? are there
any more efficient ways of doing this?
--
View this message in context: http://www.nabble.com/what-if-i-need...html#a12313472
Sent from the MySQL - General mailing list archive at Nabble.com.

  Réponse avec citation
Vieux 24/08/2007, 15h48   #2 (permalink)
Martijn Tonies
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?



>
> what if i need the primary key of a row that is to be inserted? that

primary
> key will be used to insert another row from another table.. what i was

doing
> was after inserting the row, i get the primary key by select statement and
> use that value to insert a row from another table.. is this ok? are there
> any more efficient ways of doing this?


I take it you mean a "primary key value that comes from an auto-increment
column"?

Next, we have to assume you're doing this:

insert into mytable ( ... ) values ( ... )

select max(ID-column) from mytable

re-use the value to insert child records?

Is that correct?

If so, I'd say this will ONLY work properly in a multi-user system if
you're using transactions and you don't commit between the actual
INSERT and SELECT.


It's a pity that MySQL doesn't understand the INSERT INTO ... RETURNING
syntax, as this would solve your problem :-)

Martijn Tonies
Database Workbench - development tool for MySQL, and more!
Upscene Productions
http://www.upscene.com
My thoughts:
http://blog.upscene.com/martijn/
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com

  Réponse avec citation
Vieux 24/08/2007, 15h52   #3 (permalink)
hardc0d3r
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?


yup, that's it.. thanks for the reply..
--
View this message in context: http://www.nabble.com/what-if-i-need...html#a12313942
Sent from the MySQL - General mailing list archive at Nabble.com.

  Réponse avec citation
Vieux 24/08/2007, 16h04   #4 (permalink)
Martijn Tonies
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?


> >> what if i need the primary key of a row that is to be inserted? that

> > primary
> >> key will be used to insert another row from another table.. what i
> >> was

> > doing
> >> was after inserting the row, i get the primary key by select
> >> statement and use that value to insert a row from another table.. is
> >> this ok? are there any more efficient ways of doing this?

> >
> > I take it you mean a "primary key value that comes from an
> > auto-increment column"?
> >
> > Next, we have to assume you're doing this:
> >
> > insert into mytable ( ... ) values ( ... )
> >
> > select max(ID-column) from mytable
> >
> > re-use the value to insert child records?
> >
> > Is that correct?
> >
> > If so, I'd say this will ONLY work properly in a multi-user system if
> > you're using transactions and you don't commit between the actual
> > INSERT and SELECT.
> >
> >
> > It's a pity that MySQL doesn't understand the INSERT INTO ...
> > RETURNING syntax, as this would solve your problem :-)
> >

>
> i agree that if he's trying to select the highest value in some manner
> as you describe he's in trouble, but there is of course
> "last_insert_id()", which does solve his problem.
> <http://dev.mysql.com/doc/refman/5.1/...ons.html#funct
> ion_last-insert-id>


Ah yes, I was searching for that in the docs but couldn't remember it :-)

Thanks!

Martijn Tonies
Database Workbench - tool for InterBase, Firebird, MySQL, NexusDB, Oracle &
MS SQL Server
Upscene Productions
http://www.upscene.com
My thoughts:
http://blog.upscene.com/martijn/
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com

  Réponse avec citation
Vieux 24/08/2007, 16h36   #5 (permalink)
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
Vieux 24/08/2007, 21h43   #6 (permalink)
Rudy Lippan
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 Fri, 24 Aug 2007, Michael Dykman wrote:

> calculating the max key + 1 , as it has been noted, must be done
> transactionally and will therefore require a transactional table


And with a transaction isolation level greater than Read Commited...

-r
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h50.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11155 seconds with 14 queries