http://www.perlmonks.org?node_id=67620


in reply to DBI and primary keys

This really, really depends on the database you're using. MySQL uses AUTO_INCREMENT fields and DBD::mysql stores the value for the row just inserted in $dbh->{mysql_insertid}.

Sybase and MS SQL Server use IDENTITY fields, which stores the value in your database connection session and you can retrieve the value using SELECT @@IDENTITY.

Postgres uses a sequence to accomplish the same thing and you can either select the NEXTVAL from the sequence before you send the statement to the db and send the ID value or, if you use the SERIAL datatype you can SELECT CURRVAL( sequence-name ) and get the current sequence value.

IIRC, Oracle uses sequences as well but I don't know the exact semantics.

Or (plug plug) you can use SPOPS, which does this and a number of other things for you. Postgres support is in CVS and will be in the next version.

Chris
M-x auto-bs-mode

Replies are listed 'Best First'.
Re: Re: DBI and primary keys
by BigJoe (Curate) on Mar 28, 2001 at 02:01 UTC
    To get the number from a sequence in Oracle you can do a quick select like:
    my $sth = $dbh->prepare("select MY_SEQUENCE.NEXTVAL from DUAL");
    and this will give you the next number from Oracle. Dual is a magic table. It is there to ask the Database different questions.

    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.
    Use the source Luke.