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 Database forums, part of the Database Forums category; This seems like it would have a very common answer, I just haven't stumbled upon it. Apologies in advance ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008
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!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008
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
Reply With Quote
  #3 (permalink)  
Old 02-28-2008
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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:32 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0