Home > Ask the Oracle Experts > SQL Questions & Answers > Gaps in autonumber sequences
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Gaps in autonumber sequences

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 09 March 2004
If I have a database with a table that has a primary key, and let's say it currently has 22 records (each with a primary key that is incremented by one, i.e. 1-22), but at one time there were 23 records. Ordinarily the next time a record is added the next primary key will be 24 since there was once a 23. Is there a way to force this to start again at 23 rather than 24? I don't think just making an identity seed of 23 will work, correct?

>
EXPERT RESPONSE

There are ways to manipulate the values assigned by an autonumber or identity or auto_increment or sequence. The method varies from database to database, but there is one thing every method has in common: don't do it!!

An autonumber or identity or auto_increment or sequence column has an important characteristic: used as a surrogate key, it has no intrinsic meaning. No part of your application should care what the value is, and most importantly, you should never hardcode the value. It would be bad practice to write a query such as:

select count(*) as WidgetOrders
  from orders
 where orders.productid = 23 -- widgets

Your application should never even let the users see the surrogate key values, even though it may not always be possible to keep them totally hidden. For example, consider this form field:

Select Product:

This form field is created by this HTML:

<select name="productid">
<option value="21">doodad</option>
<option value="22">gadget</option>
<option value="24">gizmo</option> 
</select>

This HTML is dynamically generated from the results of this query:

select id
     , descr
  from products
order by descr

Note that Web page users don't actually see the id values unless they know how to view and read HTML. Might they not see the gap? Yes, and someone who knows how to spoof an HTML form might try to submit a productid value of 23, but your server side logic should immediately reject it, by simply looking it up in the products table to ensure it's an existing key value.

The situation is even worse when you consider deletions. If you have sequential numbers from 1 through 937, and then delete number 21, how many rows do you have to update in order to "remove" the gap? How long will that take? And what if there's another table with a relationship to the surrogate primary key? All the foreign key values in the other table will have to be renumbered as well. Yikes!

There is nothing to be gained by renumbering surrogate keys. Leave them alone, and your application will continue to work nicely. There's no benefit to having a gap-free sequence, but trying to maintain a gap-free sequence takes work and exposes you to data integrity problems. If you think you have a good reason, please write to me, because there's usually a better way to achieve whatever you think you need a gap-free sequence for.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL
Which normal form is used most?
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts