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

Home -> Community -> Mailing Lists -> Oracle-L -> Re: sequences

Re: sequences

From: Natasha Batson <nbatson_at_neal-and-massy.com>
Date: Wed, 12 Jul 2000 13:56:32 -0400
Message-Id: <10556.111845@fatcity.com>


This is a multi-part message in MIME format.

------=_NextPart_000_0539_01BFEC08.FBEBEEA0 Content-Type: text/plain;

        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi Glen

Just some basic info.

With regards to the use of sequences which are actually separate Oracle = objects like
tables and indexes, they are used in INSERT statements along with the = pseudocolumn
NEXTVAL which generates a new sequence number or CURRVAL which says = which is
the last sequence value used.

e.g. To create a sequence that will start with the value of 1000 and = increment by 1
such that the sequence is 1000, 1001, 1002, etc. this could be the SQL = statement used
( there are other options which you could check in your SQL Reference = manual or=20
Oracle CD documentation ) :

CREATE SEQUENCE employee_empno_seq
START WITH 1000
INCREMENT BY 1
NOCACHE;=20
( by default Oracle will generate 20 sequence values and place them in = the cache for
future use by users )

The sequence could then be referenced in an INSERT statement using = NEXTVAL e.g:
INSERT INTO employee( empno, name )
VALUES (employee_empno.NEXTVAL, 'SMITH');

And this would generate the next value in the sequence and insert it = into the empno column
of the employee table. Since we didn't use the sequence yet, = employee_empno.NEXTVAL
would generate the value 1000. And if we were to reference the sequence = again in another
INSERT statement e.g.

INSERT INTO employee( empno, ename )
VALUES ( employee_empno.NEXTVAL, 'GREEN');

A row with the values 1001 for the empno and GREEN for the ename would = now be inserted.
So each time NEXTVAL is referenced it generates a new unique value for = the sequence. Follow?

Hope this info helped : )

Regards
Natasha

  I wanted to ask how people are handling the insertion of unique keys = in Oracle. Please understand that my experience with generating unique = keys comes from an Informix background, wherein you can have a column = with datatype of 'serial'. This is an integer column which gets = generated at insert time with the next value automatically. You can = then check the sqlca area (return buffer) for the inserted value.   =20
  I know in Oracle you can accomplish this with the SEQUENCE function. = As this is not automatically inserted by Oracle, how is the unqiue key = value inserted? Do most people use a before trigger, stored procs, or = call the sequence themselves? If using a client program (c, java), how = is the value for the unique key returned to the program which performed = the insert?

  Examples, methods, design recommendations welcome...

------=_NextPart_000_0539_01BFEC08.FBEBEEA0 Content-Type: text/html;

        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2919.6307" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi Glen</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Just some basic info.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>With =
regards to the=20
use of sequences which are actually separate Oracle objects=20 like</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>tables =
and indexes,=20
they are used in INSERT statements along with the=20 pseudocolumn</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D770075615-12072000>NEXTVAL which=20 generates a new sequence number or CURRVAL which says which=20 is</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>the =
last sequence=20
value used.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D770075615-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>e.g. =
To create a=20
sequence that will start with the value of 1000 and increment by=20 1</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>such =
that the=20
sequence is 1000, 1001, 1002, etc. this could be the SQL statement=20 used</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>( =
there&nbsp;are=20
other options which you could check in your SQL Reference manual or=20
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>Oracle =

CD&nbsp;documentation ) :</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D770075615-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>CREATE =
SEQUENCE=20
employee_empno_seq</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>START =
WITH=20
1000</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D770075615-12072000>INCREMENT BY=20 1</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D770075615-12072000>NOCACHE;=20
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>( by =
default Oracle=20
will generate 20 sequence values and place them in the cache=20 for</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>future =
use by users=20
)</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>The sequence could then be referenced in an INSERT statement using =
NEXTVAL=20
e.g:</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>INSERT =
INTO=20
employee( empno, name )</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>VALUES =

(employee_empno.NEXTVAL, 'SMITH');</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D770075615-12072000></SPAN></FONT>&nbsp;</DIV>
<DIV>And this would generate the next value in the sequence and insert =
it into=20
the empno column</DIV>
<DIV>of the employee table. Since we didn't use the sequence=20
yet,&nbsp;employee_empno.NEXTVAL</DIV>
<DIV>would generate the value 1000. And if we were to reference the =
sequence=20
again in another</DIV>
<DIV>INSERT statement e.g.</DIV>
<DIV>&nbsp;</DIV>
<DIV>INSERT INTO employee( empno, ename )</DIV>
<DIV>VALUES ( employee_empno.NEXTVAL, 'GREEN');</DIV>
<DIV>&nbsp;</DIV>
<DIV>A&nbsp;row with the values 1001 for the empno and GREEN for the =
ename would=20
now be inserted.</DIV>
<DIV>So each time NEXTVAL is referenced it generates a new unique value =
for the=20
sequence. Follow?</DIV>
<DIV>&nbsp;</DIV>
<DIV>Hope this info helped : )</DIV>
<DIV>&nbsp;</DIV>
<DIV>Regards</DIV>
<DIV>Natasha</DIV>
<DIV>&nbsp;</DIV></FONT></DIV>
<BLOCKQUOTE=20

style=3D"BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: = 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">   <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>   <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: = black"><B>From:</B>=20
  <A href=3D"mailto:Glenn.Travis_at_wcom.com" = title=3DGlenn.Travis_at_wcom.com>Glenn=20
  Travis</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A = href=3D"mailto:ORACLE-L_at_fatcity.com"=20
  title=3DORACLE-L_at_fatcity.com>Multiple recipients of list ORACLE-L</A> =
</DIV>

  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Wednesday, July 12, 2000 = 1:23=20
  PM</DIV>

  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> sequences</DIV>
  <DIV><BR></DIV>
  <DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>I =
wanted to ask=20
  how people are handling the insertion of unique keys in Oracle.&nbsp; = Please=20
  understand that my experience with generating unique keys comes from = an=20
  Informix background, wherein you can have a column with datatype of=20   'serial'.&nbsp; This is an integer column which gets generated at = insert time=20
  with the next value automatically.&nbsp; You can then check the sqlca = area=20
  (return buffer) for the inserted value.</SPAN></FONT></DIV>   <DIV><FONT face=3DArial size=3D2><SPAN=20   class=3D770075615-12072000></SPAN></FONT>&nbsp;</DIV>   <DIV><FONT face=3DArial size=3D2><SPAN class=3D770075615-12072000>I = know in Oracle=20
  you can accomplish this with the SEQUENCE function.&nbsp; As this is = not=20
  automatically inserted by Oracle, how is the unqiue key value = inserted?&nbsp;=20
  Do most people use&nbsp;a before trigger, stored procs, or call the = sequence=20
  themselves?&nbsp; If using a client program (c, java), how is the = value for=20
  the unique key returned to the program which performed the=20   insert?</SPAN></FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2><SPAN = Received on Wed Jul 12 2000 - 12:56:32 CDT

Original text of this message

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