SlideShare a Scribd company logo
1 of 1
Download to read offline
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

What's hot (9)

Subqueries
SubqueriesSubqueries
Subqueries
 
Sub query example with advantage and disadvantages
Sub query example with advantage and disadvantagesSub query example with advantage and disadvantages
Sub query example with advantage and disadvantages
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
Oracle Applications Alerts
Oracle Applications AlertsOracle Applications Alerts
Oracle Applications Alerts
 
Oracle alert
Oracle alertOracle alert
Oracle alert
 
Beier hints
Beier hintsBeier hints
Beier hints
 
Application sql issues_and_tuning
Application sql issues_and_tuningApplication sql issues_and_tuning
Application sql issues_and_tuning
 
Trigger in mysql
Trigger in mysqlTrigger in mysql
Trigger in mysql
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 

Similar to Autonumber

Access tips access and sql part 3 practical examples
Access tips  access and sql part 3  practical examplesAccess tips  access and sql part 3  practical examples
Access tips access and sql part 3 practical examplesquest2900
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 
SQL Database Performance Tuning for Developers
SQL Database Performance Tuning for DevelopersSQL Database Performance Tuning for Developers
SQL Database Performance Tuning for DevelopersBRIJESH KUMAR
 
JPQL/ JPA Activity 1
JPQL/ JPA Activity 1JPQL/ JPA Activity 1
JPQL/ JPA Activity 1SFI
 
Sql modifying data - MYSQL part I
Sql modifying data - MYSQL part ISql modifying data - MYSQL part I
Sql modifying data - MYSQL part IAhmed Farag
 
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...shalinigambhir3
 
9 - Advanced Functions in MS Excel.pptx
9 - Advanced Functions in MS Excel.pptx9 - Advanced Functions in MS Excel.pptx
9 - Advanced Functions in MS Excel.pptxSheryldeVilla2
 
Ben Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptxBen Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptxStephenEfange3
 
Mysql clone-tables
Mysql clone-tablesMysql clone-tables
Mysql clone-tablesbeben benzy
 
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008paulguerin
 
JPQL/ JPA Activity 2
JPQL/ JPA Activity 2JPQL/ JPA Activity 2
JPQL/ JPA Activity 2SFI
 
An invisible oracle bug
An invisible oracle bugAn invisible oracle bug
An invisible oracle bugsantosh challa
 

Similar to Autonumber (20)

Migration
MigrationMigration
Migration
 
1 introduction to my sql
1 introduction to my sql1 introduction to my sql
1 introduction to my sql
 
Database
DatabaseDatabase
Database
 
Access tips access and sql part 3 practical examples
Access tips  access and sql part 3  practical examplesAccess tips  access and sql part 3  practical examples
Access tips access and sql part 3 practical examples
 
Columnrename9i
Columnrename9iColumnrename9i
Columnrename9i
 
Columndrop
ColumndropColumndrop
Columndrop
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
 
SQL Database Performance Tuning for Developers
SQL Database Performance Tuning for DevelopersSQL Database Performance Tuning for Developers
SQL Database Performance Tuning for Developers
 
JPQL/ JPA Activity 1
JPQL/ JPA Activity 1JPQL/ JPA Activity 1
JPQL/ JPA Activity 1
 
Sql modifying data - MYSQL part I
Sql modifying data - MYSQL part ISql modifying data - MYSQL part I
Sql modifying data - MYSQL part I
 
Mysql cheatsheet
Mysql cheatsheetMysql cheatsheet
Mysql cheatsheet
 
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
 
9 - Advanced Functions in MS Excel.pptx
9 - Advanced Functions in MS Excel.pptx9 - Advanced Functions in MS Excel.pptx
9 - Advanced Functions in MS Excel.pptx
 
Ben Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptxBen Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptx
 
Mysql clone-tables
Mysql clone-tablesMysql clone-tables
Mysql clone-tables
 
Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008Myth busters - performance tuning 102 2008
Myth busters - performance tuning 102 2008
 
How to Use VLOOKUP in Excel
How to Use VLOOKUP in ExcelHow to Use VLOOKUP in Excel
How to Use VLOOKUP in Excel
 
JPQL/ JPA Activity 2
JPQL/ JPA Activity 2JPQL/ JPA Activity 2
JPQL/ JPA Activity 2
 
An invisible oracle bug
An invisible oracle bugAn invisible oracle bug
An invisible oracle bug
 

More from oracle documents (20)

Applyinga blockcentricapproachtotuning
Applyinga blockcentricapproachtotuningApplyinga blockcentricapproachtotuning
Applyinga blockcentricapproachtotuning
 
Windowsosauthent
WindowsosauthentWindowsosauthent
Windowsosauthent
 
Whatistnsnames
WhatistnsnamesWhatistnsnames
Whatistnsnames
 
Whatisadatabaselink
WhatisadatabaselinkWhatisadatabaselink
Whatisadatabaselink
 
Varraysandnestedtables
VarraysandnestedtablesVarraysandnestedtables
Varraysandnestedtables
 
Usertracing
UsertracingUsertracing
Usertracing
 
Userpasswrd
UserpasswrdUserpasswrd
Userpasswrd
 
Userlimit
UserlimitUserlimit
Userlimit
 
Undo internalspresentation
Undo internalspresentationUndo internalspresentation
Undo internalspresentation
 
Undo internals paper
Undo internals paperUndo internals paper
Undo internals paper
 
Tablespacelmt
TablespacelmtTablespacelmt
Tablespacelmt
 
Tablerename
TablerenameTablerename
Tablerename
 
Sql scripting sorcerypresentation
Sql scripting sorcerypresentationSql scripting sorcerypresentation
Sql scripting sorcerypresentation
 
Sql scripting sorcerypaper
Sql scripting sorcerypaperSql scripting sorcerypaper
Sql scripting sorcerypaper
 
Sql for dbaspresentation
Sql for dbaspresentationSql for dbaspresentation
Sql for dbaspresentation
 
Sequencereset
SequenceresetSequencereset
Sequencereset
 
Rollbacksizes
RollbacksizesRollbacksizes
Rollbacksizes
 
Rollbackshrinks
RollbackshrinksRollbackshrinks
Rollbackshrinks
 
Rollbacklmt
RollbacklmtRollbacklmt
Rollbacklmt
 
Rollbackblocking
RollbackblockingRollbackblocking
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