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: An Autonumber field in Oracle

Re: An Autonumber field in Oracle

From: DA Morgan <damorgan_at_x.washington.edu>
Date: Fri, 25 Feb 2005 08:41:16 -0800
Message-ID: <1109349497.608158@yasure>


Lars Gr?tteland wrote:

> Hello!
>
> I'm a newbie to oracle and wondering how to have a autonumber field in
> Oracle. Do I have to set a trigger to that item?
>
> Temp_ID Name
> 1
> 2
> 3
> 4
> 5
> 6
> and so on. How do I do that?
>
> - Lars

Oracle, thankfully, does not support autonumbering fields. The Oracle method involves using a sequence and can be implemented as follows:

CREATE TABLE mytable (
mycolumn NUMBER);

CREATE SEQUENCE myseq;

CREATE OR REPLACE TRIGGER mytrigger
BEFORE INSERT
ON mytable
FOR EACH ROW BEGIN
    SELECT myseq.NEXTVAL
    INTO :NEW.mycolumn
    FROM dual;
END mytrigger;
/

insert into mytable values (99);
insert into mytable values (99);
insert into mytable values (99);

SELECT * FROM mytable;

-- 
Daniel A. Morgan
University of Washington
damorgan_at_x.washington.edu
(replace 'x' with 'u' to respond)
Received on Fri Feb 25 2005 - 10:41:16 CST

Original text of this message

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