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

Home -> Community -> Usenet -> c.d.o.misc -> Re: Newbie question: Autonumber

Re: Newbie question: Autonumber

From: DA Morgan <damorgan_at_exesolutions.com>
Date: Mon, 10 Feb 2003 15:11:27 -0800
Message-ID: <3E48319F.89B2369C@exesolutions.com>


Eirik Eldorsen wrote:

> How can i create a coloumn in a table, that has an autogenerated uniqe
> number? Im connecting to a Oracle 8i server
>
> CREATE TABLE EMPLOYEE(
> EmployeeNr AUTONUMBER NOT NULL,
> Name VARCHAR2(50),
> CONSTRAINT EmployeeNr_pk PRIMARY KEY(EmployeeNr));
>
> --
> Eirik Eldorsen

Oracle does not have autonumbering columns something of which we are all quite happy. All of the weaknesses in these columns are solved by Oracle's object called a SEQUENCE. You can learn about them at http://tahiti.oracle.com but in its simplest form you create the sequence and then use a BEFORE-INSERT trigger to put the number into the table or just request the next numeric value from the sequence in your insert statement. Here are a few samples to help you get started.

CREATE TABLE xyz (

   id_column NUMBER(5));

CREATE SEQUENCE seq_xyz;

INSERT INTO xyz
(id_column)
VALUES
(seq_xyz.NEXTVAL);

Daniel Morgan Received on Mon Feb 10 2003 - 17:11:27 CST

Original text of this message

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