Re: Auto-Increment the ID of a table
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
Subject
Written By
Posted
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.