Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
No such thing as a small change
 
PerlMonks  

Re: DBI and primary keys

by lachoy (Parson)
on Mar 27, 2001 at 19:21 UTC ( [id://67620]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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 27, 2001 at 21: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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://67620]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.