Gossamer Forum
Home : General : Databases and SQL :

inserting into multiple with fk auto_increment

Quote Reply
inserting into multiple with fk auto_increment
If I have a subroutine that needs to add a row to each of two related tables, where the auto_increment primary key of one is used as a foreign key in the other, what's the best way to make sure that the same value is added for both fields? Currently I'm inserting NULL for the pk value in the first table, because I have no way (that I know of) of determining what should be the next auto_increment value.

Any thoughts? Thanks in advance.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] inserting into multiple with fk auto_increment In reply to
When you do an INSERT you can retrieve the auto-incremented value using LAST_INSERT_ID()

Last edited by:

Paul: Mar 7, 2003, 12:55 PM
Quote Reply
Re: [Paul] inserting into multiple with fk auto_increment In reply to
Cool - thanks.

What parameters can be fed to that function? Can I use LAST_INSERT_ID(dbname.tablename) for example?

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund

Last edited by:

hennagaijin: Mar 7, 2003, 2:26 PM