MySQL Forums
Forum List  »  Newbie

Re: Auto-Increment the ID of a table
Posted by: Felix Geerinckx
Date: April 29, 2005 12:44PM

Jason M wrote:

> I've set the ID column to auto-increment, but I am still going to need to select that last row added
> afterwards so that I can use that value for the ID value of my second table. I hope that makes sense.

You need LAST_INSERT_ID():

CREATE TABLE foo1 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE foo2 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, foo1_id INT NOT NULL);

INSERT INTO foo1 (id) VALUES(NULL);
INSERT INTO foo2 (id, foo1_id) VALUES(NULL, LAST_INSERT_ID());

--
felix

Options: ReplyQuote


Subject
Written By
Posted
April 29, 2005 11:41AM
Re: Auto-Increment the ID of a table
April 29, 2005 12:44PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.