Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: An Autonumber field in Oracle
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