I'm subsequently inserting a single record in two tables. With the first insert i put the record in the main table, where the primary key value gets generated. Now i need to put some fields of this record in another table including the key value of the main table. How do i do this?
views:
64answers:
1
+3
A:
"SELECT LAST_INSERT_ID();
" will return the last autoincrement ID generated.
Jorge Villuendas
2009-03-01 17:29:50
And a thread-safe way to do this?
akosch
2009-03-01 17:33:56
LAST_INSERT_ID() is thread-safe. It's stored on a per-connection basis, so insert statements from others connections will not affect its value.
Jorge Villuendas
2009-03-01 17:41:56
If akosch is sharing connections between threads (yuck) the insert may need to be in a stored function that returns the last_insert_id().
ysth
2009-03-01 23:30:54
Well, I would better redesign the application to not share the same opened connection between several threads, unless he has a good reason for doing so.
Jorge Villuendas
2009-03-02 15:07:58