On 23/04/2009, at 12:53 AM, Joseph Schmidt wrote:

Of course I demonstrated. The snippet in the parent just works with MySQL (with InnoDB of course). There's no need for a last_insert_id(), since the PK are "autoincrement". Inside the same transaction, the consequent selects will simply pick the correct values (generated by autoincrement). This is how MySQL is mostly used.

I don't want to get into an argument about this, but I'd like this to be clear in case other people are reading the archives of this thread in the future. You proposed this:

No if one is doing with DB directly or with JDBC, the following will work:
-- start transaction
INSERT INTO t_1 (name) VALUES ('name1');
INSERT INTO t_1 (name) VALUES ('name2');
INSERT INTO t_2 (name,t_1_id) select 'name1' , t_1.id from t_1 where t_1.name='name1';
commit; -- end transaction

That doesn't work unless t_1.name is unique for that table. And if it is, then you have no need for t_1.id because you are effectively generating your own primary key. The only way I know of in mysql to get the primary key of the last insert (which is basically what you are trying to do) is use last_insert_id().

Ari


-------------------------->
ish
http://www.ish.com.au
Level 1, 30 Wilson Street Newtown 2042 Australia
phone +61 2 9550 5001   fax +61 2 9550 4001
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A


Reply via email to