SlideShare a Scribd company logo
Autonumber fields                                                               Administration Tips




Autonumber fields in Oracle

Microsoft Access allows a column in a table to be declared as an 'autonumber' field, which
guarantees uniqueness for every row inserted (what I believe is termed a 'synthetic'
primary key), although you can also use it in any column, even if it isn't the primary key of
the table. SQL Server has something very similar, called 'increment'.

The question arises: is there such a feature in Oracle? To which the reply is "no, but you
can fudge it".

In Oracle, you are going to need to access a Sequence, which will generate the unique
numbers for you. The basic command would be:

CREATE SEQUENCE SEQ_BLAH;


But that's not enough: you probably want the next sequence number to be extracted and
inserted automatically whenever a new record is inserted. For that, you'll have to create a
trigger on the relevant table, like this:

CREATE OR REPLACE TRIGGER BLAH_AUTO
BEFORE INSERT ON TABLE_NAME
FOR EACH ROW
BEGIN
         SELECT SEQ_BLAH.NEXTVAL
         INTO   :NEW.COLUMN_NAME   FROM DUAL;
END;
/

The trigger will fire any time you try to insert a new record into table table_name, and
will grab the next number from the seq_blah sequence we created earlier. It assigns that
to the relevant column as its new value (even if one is manually supplied by the User -the
sequence value overrides anything a User submits).

One thing to watch out for: because of the way Oracle caches sequences, it is NOT
guaranteed that all autonumbers generated in this way will be in sequence without a any
gaps. In other words, a subsequent select on the relevant table might reveal auto-
generated numbers to be 1,2,4,6,7,11 and so on. All numbers are guaranteed unique, but
you can do nothing about the potential gaps. If that's a problem for you, you'll have to
think of another method (such as a separate table containing the seed number from which
inserts select the next number and increment it -if the original insert fails, the update to
the seed number fails. This method introduces potentially awful contention, locking and
performance issues, however). Otherwise, this rough-and-ready fudge will do the trick.




Copyright © Howard Rogers 2001                  10/17/2001                               Page 1 of 1

More Related Content

DOCX
10053 - null is not nothing
DOCX
Not in vs not exists
PPT
SQL subquery
PPTX
Guide To Mastering The MySQL Query Execution Plan
ODP
Performance tuning
PDF
Sql query tips or query optimization
PPTX
MySQL index optimization techniques
DOCX
Alerts in r12
10053 - null is not nothing
Not in vs not exists
SQL subquery
Guide To Mastering The MySQL Query Execution Plan
Performance tuning
Sql query tips or query optimization
MySQL index optimization techniques
Alerts in r12

What's hot (9)

PPTX
Sub query example with advantage and disadvantages
PDF
5. Group Functions
PPT
Oracle Applications Alerts
PDF
Oracle alert
PPT
Beier hints
PDF
Application sql issues_and_tuning
PPTX
Trigger in mysql
Sub query example with advantage and disadvantages
5. Group Functions
Oracle Applications Alerts
Oracle alert
Beier hints
Application sql issues_and_tuning
Trigger in mysql
Ad

Similar to Autonumber (20)

PPTX
Oracle Database Sequence
PDF
SEQUNCES Lecture_Notes_Unit4_chapter11_sequence
PPT
Database Objects
PPT
PPT
Sequences
PPTX
Sql fundamentals
PPT
Sequences and indexes
PPT
Dbms oracle
PPT
Les13[1]Other Database Objects
PPT
plsql Les09
PPT
Creating and Managing Tables -Oracle Data base
PPTX
12 things about Oracle 12c
PPT
PPTX
429cf300-0dc7-4c2e-9280-d918d69e3cb4.pptx
PDF
Polymorphic Table Functions in SQL
PPT
DDL. data defination language for creating database
PPTX
Oracle 11g new features for developers
PDF
Oracle Database 12c Application Development
Oracle Database Sequence
SEQUNCES Lecture_Notes_Unit4_chapter11_sequence
Database Objects
Sequences
Sql fundamentals
Sequences and indexes
Dbms oracle
Les13[1]Other Database Objects
plsql Les09
Creating and Managing Tables -Oracle Data base
12 things about Oracle 12c
429cf300-0dc7-4c2e-9280-d918d69e3cb4.pptx
Polymorphic Table Functions in SQL
DDL. data defination language for creating database
Oracle 11g new features for developers
Oracle Database 12c Application Development
Ad

More from oracle documents (20)

PPT
Applyinga blockcentricapproachtotuning
PDF
Windowsosauthent
PDF
Whatistnsnames
PDF
Whatisadatabaselink
PDF
Varraysandnestedtables
PDF
Usertracing
PDF
Userpasswrd
PDF
Userlimit
PDF
Undo internalspresentation
PDF
Undo internals paper
PDF
Tablespacelmt
PDF
Tablerename
PDF
Sql scripting sorcerypresentation
PDF
Sql scripting sorcerypaper
PDF
Sql for dbaspresentation
PDF
Sequencereset
PDF
Rollbacksizes
PDF
Rollbackshrinks
PDF
Rollbacklmt
PDF
Rollbackblocking
Applyinga blockcentricapproachtotuning
Windowsosauthent
Whatistnsnames
Whatisadatabaselink
Varraysandnestedtables
Usertracing
Userpasswrd
Userlimit
Undo internalspresentation
Undo internals paper
Tablespacelmt
Tablerename
Sql scripting sorcerypresentation
Sql scripting sorcerypaper
Sql for dbaspresentation
Sequencereset
Rollbacksizes
Rollbackshrinks
Rollbacklmt
Rollbackblocking

Autonumber

  • 1. Autonumber fields Administration Tips Autonumber fields in Oracle Microsoft Access allows a column in a table to be declared as an 'autonumber' field, which guarantees uniqueness for every row inserted (what I believe is termed a 'synthetic' primary key), although you can also use it in any column, even if it isn't the primary key of the table. SQL Server has something very similar, called 'increment'. The question arises: is there such a feature in Oracle? To which the reply is "no, but you can fudge it". In Oracle, you are going to need to access a Sequence, which will generate the unique numbers for you. The basic command would be: CREATE SEQUENCE SEQ_BLAH; But that's not enough: you probably want the next sequence number to be extracted and inserted automatically whenever a new record is inserted. For that, you'll have to create a trigger on the relevant table, like this: CREATE OR REPLACE TRIGGER BLAH_AUTO BEFORE INSERT ON TABLE_NAME FOR EACH ROW BEGIN SELECT SEQ_BLAH.NEXTVAL INTO :NEW.COLUMN_NAME FROM DUAL; END; / The trigger will fire any time you try to insert a new record into table table_name, and will grab the next number from the seq_blah sequence we created earlier. It assigns that to the relevant column as its new value (even if one is manually supplied by the User -the sequence value overrides anything a User submits). One thing to watch out for: because of the way Oracle caches sequences, it is NOT guaranteed that all autonumbers generated in this way will be in sequence without a any gaps. In other words, a subsequent select on the relevant table might reveal auto- generated numbers to be 1,2,4,6,7,11 and so on. All numbers are guaranteed unique, but you can do nothing about the potential gaps. If that's a problem for you, you'll have to think of another method (such as a separate table containing the seed number from which inserts select the next number and increment it -if the original insert fails, the update to the seed number fails. This method introduces potentially awful contention, locking and performance issues, however). Otherwise, this rough-and-ready fudge will do the trick. Copyright © Howard Rogers 2001 10/17/2001 Page 1 of 1