Oracle - Create an autonumber field


Written on January 6, 2008 – 4:41 am | by André Gomes

If you want to create an autoincrements field in a table, using Oracle, you may do this by using sequences.

What are sequences?

Well, sequence is a command that gives to you the power to do this.

Nothing better than an example:

create table user
(   id number(10),
    name varchar2(32),
    primary key(id)
);

If you want the id field increase automatically, you use the sequence:

create sequence user_seq
start with 1
increment by 1
nomaxvalue;

Naturally, as you can see, when new records are entry on table, the id field will be increasing.

insert into user values(user_seq.nextval, 'andre');

select * from user

… Experiment yourself now :)

Just for curiosity, the sintaxe of sequence is:

CREATE SEQUENCE sequence_name
    MINVALUE value
    MAXVALUE value
    START WITH value
    INCREMENT BY value
    CACHE value;


Veja também:

Tags: , ,

  1. 2 Responses to “Oracle - Create an autonumber field”

  2. By nunojob on Jan 6, 2008 | Reply

    Good article ;) Congrats! =)

  1. 1 Trackback(s)

  2. Jan 6, 2008: links for 2008-01-06 « .$null@dscape/07

Post a Comment