Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: DBI and primary keys

by lachoy (Parson)
on Mar 28, 2001 at 00:21 UTC ( #67620=note: print w/ replies, xml ) Need Help??


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


Comment on Re: DBI and primary keys
Select or Download Code
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://67620]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-09-30 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (384 votes), past polls