Re: Primary Key and autonumber
Date: 2000/01/05
Message-ID: <23jVNXA0l8c4Ewct_at_shrdlu.com>#1/1
In article <38735E63.581D1E1C_at_erols.com>, Francois Merle
<leurmy_at_erols.com> writes
>Let's assume I have a database that has 1 million records, all uniquely
>identified with a Primary Key.
This is a unique key.
>If my database does not provide an Autonumber for the Primary Key, how
>can I efficiently give each new record a unique key without going
>through the entire database to check if that value has already been
>taken?
Your database already has a unique key, you have already told us that.
What you can do is make sure that the index on that key doesn't accept duplicates. If you do that then any record you succeed in adding will have a unique key.
If you want to add an additional unique key then:
- Add a new numeric field to the table. Perhaps call it ID.
- Write a program that writes the record number into that field
- Create a new table with one numeric field and one record
- Write a number 1 greater than the greatest ID value
- When you need to add a new record
- Retrieve the number from the new table
- Add 1
- Save this new number to overwrite the old one
- Use the new number as the ID in your main table
It's important to do this in the right order. The most likely failure mode will leave gaps in the numeric sequence. If you use this to generate invoice numbers the auditors might get suspicious about the gaps.
-- Bernard Peek bap_at_shrdlu.com bap_at_shrdlu.co.ukReceived on Wed Jan 05 2000 - 00:00:00 CET