Oracle - Create an autonumber field

67
rate or flag this page

By aslgomes


Before anything, i want to say that this article was posted on my personal blog, and i only put it here because the chances that this article possible be useful for anyone are greater with HubPages.

Now, take a look:

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;

This post is on my personal blog too, in portuguese languange, here.


Print   —   Rate it:  up  down  flag this hub

Comments

RSS for comments on this Hub

surender sara  says:
11 months ago

I would suggest to take a look at free Oracle DBA and APPS DBA Training at

http://www.orabyte.com/eLearning2/index.php?target

There are 100's of free vidoes and 1000+ scripts in the DBA and APPS dba tool at

http://www.orabyte.com/eLearning2/index.php?target

I use this tool and is awesome. Makes my life easy. Once you become DBA and APPS DBA you will need to get organized.

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working