Siegfried Heintze wrote:
If I use the auto-increment feature for a couple of normalized
relations, how do I insert into them?

Specifically, when I insert into a relation with the autoincrement
feature on the primary key, how do I get the value of the index on
the newly created row so I can use that the value of a foreign key in
another relation?

You can use either the last_insert_id() sql function:

  insert into parent (...) values (...);
  insert into child (id, blah) values (last_insert_id(), 'text');

Or the $dbh->{mysql_insertid} database handle attribute:

$dbh->do("insert into parent (...) values (...)");
my $lastid = $dbh->{mysql_insertid};
$dbh->do("insert into child (id, blah) values (?, 'text')", undef, $lastid);



-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to