Unix Technical Forum

How do I know the auto-generated key in a newly-inserted row?

This is a discussion on How do I know the auto-generated key in a newly-inserted row? within the MySQL forums, part of the Database Server Software category; --> This seems like it would have a very common answer, I just haven't stumbled upon it. Apologies in advance ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:49 PM
bk
 
Posts: n/a
Default How do I know the auto-generated key in a newly-inserted row?

This seems like it would have a very common answer, I just haven't
stumbled upon it. Apologies in advance for my ignorance, and thanks
in advance for your help.

Let's say I have two tables:

Authors
=-=-=-=
id (PK)
fName
lName

Books
=-=-=
id (PK)
Authors.id (FK)
title

The id fields are auto-numbered. Never mind the fact that something
like ISBN might be a better primary key for Books, etc. Let's say I
want to populate them with a Web app that will allow someone to give
an author's name and books they've written. In the form, the user
tells me that Kurt Vonnegut wrote "Slapstick".

So I guess I need to do these:

INSERT INTO Authors (fName, lName) VALUES ("Kurt", "Vonnegut");
INSERT INTO Books (Authors.id, title) VALUES (???, "Slapstick");

The ??? in the second insert is where I need help. Do I have to re-
query between these inserts in order to get the author's id in order
to insert it into Books? How do I know in the second insert what the
author's id is? What are best practices surrounding this?

Clearly, I'm a bit of a noob at this so, again, I hope my ignorance is
tolerated.

Thank you for any help!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:49 PM
Erick T. Barkhuis
 
Posts: n/a
Default Re: How do I know the auto-generated key in a newly-inserted row?

bk:

> INSERT INTO Authors (fName, lName) VALUES ("Kurt", "Vonnegut");
> INSERT INTO Books (Authors.id, title) VALUES (???, "Slapstick");
>
> The ??? in the second insert is where I need help.


In case you're using PHP, have a look at mysql_insert_id()

--
Erick
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:49 PM
Rik Wasmus
 
Posts: n/a
Default Re: How do I know the auto-generated key in a newly-inserted row?

On Thu, 28 Feb 2008 21:01:17 +0100, bk <benjamin.kann@gmail.com> wrote:

> This seems like it would have a very common answer, I just haven't
> stumbled upon it. Apologies in advance for my ignorance, and thanks
> in advance for your help.
>
> Let's say I have two tables:
>
> Authors
> =-=-=-=
> id (PK)
> fName
> lName
>
> Books
> =-=-=
> id (PK)
> Authors.id (FK)
> title
>
> The id fields are auto-numbered. Never mind the fact that something
> like ISBN might be a better primary key for Books, etc. Let's say I
> want to populate them with a Web app that will allow someone to give
> an author's name and books they've written. In the form, the user
> tells me that Kurt Vonnegut wrote "Slapstick".
>
> So I guess I need to do these:
>
> INSERT INTO Authors (fName, lName) VALUES ("Kurt", "Vonnegut");
> INSERT INTO Books (Authors.id, title) VALUES (???, "Slapstick");
>
> The ??? in the second insert is where I need help. Do I have to re-
> query between these inserts in order to get the author's id in order
> to insert it into Books? How do I know in the second insert what the
> author's id is? What are best practices surrounding this?


SELECT LAST_INSERT_ID();

--
Rik Wasmus
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:05 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com