Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: how to achieve autoincremented PKs (as datatype SERIAL does)

Re: how to achieve autoincremented PKs (as datatype SERIAL does)

From: Guido Konsolke <Guido.Konsolke_at_triaton.com>
Date: Thu, 27 Feb 2003 13:20:21 +0100
Message-ID: <1046348402.590020@news.thyssen.com>


"Frank Ratzlow" <frank_ratzlow_at_hotmail.com> schrieb im Newsbeitrag news:5ef29569.0302270357.1adad160_at_posting.google.com...
> Hallo folks,
>
> is it possible to put the handling of autogenerating PK to Oracle? I
> think to remember from Informix the datatype SERIAL that makes it
> quite easy as I don't have to determine a new value for the key.
> I just would like to insert a row without having to know or
> (calculate) the value of the new record.
> For a short time I thought of:
>
> ============
> INSERT INTO customer (customerid, firstname, lastname) VALUES ((SELECT
> MAX(customerid) FROM customer) + 1, 'Sven', 'Grundmann');
> ============
>
> but this cannot be a serious solution since it would run into trouble
> with concurrent inserts.
> Could anyone give me a hint how to implement something I only have to
> do something like
>
> INSERT INTO customer VALUES ('Sven','Grundmann');
>
> Finally, to top all this I want to execute this statment from within
> Java Code via JDBC. Is there any supported way to precalc the pk?
>
>
> TIA
>
> Frank
> frank_ratzlow_at_nojunk.hotmail.com

Hi Frank,

your PK issue can be solved by using sequences / triggers in Oracle. Just google a little and you will find examples that match your needs.

I'm not sure what you exactly mean with your second question. If you want to know the PK *before* the insert and you populate this column through a trigger, there's no way. If you want to know the value *after* the insert, well, there's the RETURNING clause. But I don't know if it is available within JAVA / JDBC.

hth,
Guido Received on Thu Feb 27 2003 - 06:20:21 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US