Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Newbie question: Autonumber
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