easy question

This is a discussion on easy question within the MySQL Database forums, part of the Database Forums category; Hello to all, I have an easy question: how can I find the first not used number in a field ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2006
gaetanoortisi@yahoo.it
 
Posts: n/a
Default easy question

Hello to all,
I have an easy question: how can I find the first not used number in a
field to use it as a primary key?

example:

id name
1 harry
2 mary
4 frankie

for the new row I have to found 3.
Thank you,

tano

Reply With Quote
  #2 (permalink)  
Old 03-01-2006
Bill Karwin
 
Posts: n/a
Default Re: easy question

<gaetanoortisi@yahoo.it> wrote in message
news:1141247390.491959.94210@i40g2000cwc.googlegro ups.com...
> I have an easy question: how can I find the first not used number in a
> field to use it as a primary key?


This is a frequent misconception, that it is important to fill the "gaps" of
a primary key column. This is almost never worth the effort, so there is
usually not a simple way to do it.

Primary keys need to be unique, but they do not need to be contiguous.

You are not likely to run out of values in an unsigned 32-bit integer. Only
if you insert 1 record every second for 136 years, will you be close to
running out.

You might be reusing primary key values that have been deleted, or which was
allocated but never committed. This is risky, because other tables in your
database might still have those values and by reusing a value that has been
deleted, you would create an implied relationship between records that
really have no relationship.

A better plan is to always assign primary key values by incrementing past
the largest value in the table. Don't try to fill the "gaps".

Even better is to use a mechanism for a monotonically-increasing counter in
the RDBMS implementation. Many RDBMS implementations have mechanisms to
automatically allocate primary key values, to reduce the risk of reusing a
value.

Anyway, in spite of all those warnings, you might have an exceptional case
where it is important to have contiguous values. Here's one type of
solution (for primary key values 0-99999):

create table d (d integer not null);
insert into d values (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);

select min(d10000.d * 10000 + d1000.d * 1000 + d100.d * 100 + d10.d * 10 +
d1.d)
from (d as d10000, d as d1000, d as d100, d as d10, d as d1)
left outer join mytable as t on t.id = (d10000.d * 10000 + d1000.d * 1000
+ d100.d * 100 + d10.d * 10 + d1.d)
where t.id is null;

Regards,
Bill K.


Reply With Quote
  #3 (permalink)  
Old 03-02-2006
gaetanoortisi@yahoo.it
 
Posts: n/a
Default Re: easy question

> > I have an easy question: how can I find the first not used number in a
> > field to use it as a primary key?

>
> This is a frequent misconception, that it is important to fill the "gaps" of
> a primary key column. This is almost never worth the effort, so there is
> usually not a simple way to do it.
>
> Primary keys need to be unique, but they do not need to be contiguous.
>


I know, and I don't want them to be contigues, I want them to be
reusable.
What I have to do is to attach (physically) to every product that is
stored
in the database a paper label that I would like to made recyclable.

>
> Even better is to use a mechanism for a monotonically-increasing counter in
> the RDBMS implementation.


I know auto-increment feature, but it's not what I need.

> Regards,
> Bill K.


Thank you a lot,
tano

Reply With Quote
  #4 (permalink)  
Old 03-04-2006
gaetanoortisi@yahoo.it
 
Posts: n/a
Default Re: easy question

thanks

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:22 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0