Unix Technical Forum

Finding lowest free key in table

This is a discussion on Finding lowest free key in table within the MySQL forums, part of the Database Server Software category; --> Hello, I have a table with staff member data. For obligatory historic reasons, each staff member has a unique ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:12 AM
anschouwe@yahoo.com
 
Posts: n/a
Default Finding lowest free key in table

Hello,

I have a table with staff member data. For obligatory historic
reasons, each staff member has a unique number assigned, which is
taken as primary key. For the same historic reasons the list of staff
numbers is not complete, i.e. there are gaps inbetween. When a new
staff member is introduced, I want to assign the lowest free number to
him or her. How can I find the lowest free integer primary key using
MySQL?

Example:

(staff_number, ...some more staff member information left off)
1
2
5
60
61
63

The lowest free staff_number is 3. Is it possible to do this entirely
in mysql?

I searched around a lot, but as I am fairly new to mysql I probably
did not search for the right terms, so I could not find enlightenment
yet.

Thanks a lot for any hint/advice/sample script!

Cheers, Pascal.


--
Das Bestreben, den Menschen gefällig zu sein,
lässt jede geistige Blüte verwelken.

("Baudolino", Umberto Eco)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:12 AM
Good Man
 
Posts: n/a
Default Re: Finding lowest free key in table

anschouwe@yahoo.com wrote in news:1184003720.436676.169760
@o61g2000hsh.googlegroups.com:

> Hello,
>
> I have a table with staff member data. For obligatory historic
> reasons, each staff member has a unique number assigned, which is
> taken as primary key. For the same historic reasons the list of staff
> numbers is not complete, i.e. there are gaps inbetween. When a new
> staff member is introduced, I want to assign the lowest free number to
> him or her. How can I find the lowest free integer primary key using
> MySQL?
>
> Example:
>
> (staff_number, ...some more staff member information left off)
> 1
> 2
> 5
> 60
> 61
> 63
>
> The lowest free staff_number is 3. Is it possible to do this entirely
> in mysql?
>
> I searched around a lot, but as I am fairly new to mysql I probably
> did not search for the right terms, so I could not find enlightenment
> yet.
>
> Thanks a lot for any hint/advice/sample script!



Why do you want to insert them at "3" and not "64"?

It seems your database was set-up to "auto-increment" the first column.
Part of the point of such a set-up is that once staff member 3 is
removed, nobody else can take that key because other tables might be
associated with it. Even if not, what benefit is there to enter with a
key of 3 versus 64?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:12 AM
Adam Englander
 
Posts: n/a
Default Re: Finding lowest free key in table

anschouwe@yahoo.com wrote:
> Hello,
>
> I have a table with staff member data. For obligatory historic
> reasons, each staff member has a unique number assigned, which is
> taken as primary key. For the same historic reasons the list of staff
> numbers is not complete, i.e. there are gaps inbetween. When a new
> staff member is introduced, I want to assign the lowest free number to
> him or her. How can I find the lowest free integer primary key using
> MySQL?
>
> Example:
>
> (staff_number, ...some more staff member information left off)
> 1
> 2
> 5
> 60
> 61
> 63
>
> The lowest free staff_number is 3. Is it possible to do this entirely
> in mysql?
>
> I searched around a lot, but as I am fairly new to mysql I probably
> did not search for the right terms, so I could not find enlightenment
> yet.
>
> Thanks a lot for any hint/advice/sample script!
>
> Cheers, Pascal.
>
>
> --
> Das Bestreben, den Menschen gefällig zu sein,
> lässt jede geistige Blüte verwelken.
>
> ("Baudolino", Umberto Eco)
>

There may be a more efficient way that someone else can come up with but
this will work:

select min(a.incid) as newid from (select id+1 as incid from test) a
where a.incid not in (select id from test);

"test" is the table holding the staff number and id is the staff number.

Adam
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:13 AM
P.A.
 
Posts: n/a
Default Re: Finding lowest free key in table

Am 09.07.2007, 21:50 Uhr, schrieb Good Man <heyho@letsgo.com>:

> anschouwe@yahoo.com wrote in news:1184003720.436676.169760
> @o61g2000hsh.googlegroups.com:
>
>> Hello,
>>
>> I have a table with staff member data. For obligatory historic
>> reasons, each staff member has a unique number assigned, which is
>> taken as primary key. For the same historic reasons the list of staff
>> numbers is not complete, i.e. there are gaps inbetween. When a new
>> staff member is introduced, I want to assign the lowest free number to
>> him or her. How can I find the lowest free integer primary key using
>> MySQL?
>>
>> Example:
>>
>> (staff_number, ...some more staff member information left off)
>> 1
>> 2
>> 5
>> 60
>> 61
>> 63
>>
>> The lowest free staff_number is 3. Is it possible to do this entirely
>> in mysql?
>>
>> I searched around a lot, but as I am fairly new to mysql I probably
>> did not search for the right terms, so I could not find enlightenment
>> yet.
>>
>> Thanks a lot for any hint/advice/sample script!

>
>
> Why do you want to insert them at "3" and not "64"?
>
> It seems your database was set-up to "auto-increment" the first column.
> Part of the point of such a set-up is that once staff member 3 is
> removed, nobody else can take that key because other tables might be
> associated with it. Even if not, what benefit is there to enter with a
> key of 3 versus 64?
>
>



Second try - my first answer didn't get through... :-/

The primary key is not set to "auto-increment", exactly for the reason you
name. Numbers that get freed shall be used later again, so the entire
entry in the table is removed when a staff member leaves. As staff members
come and go asynchronously, numbers may stay unused a while, or some more
numbers are used. The numbers are used in another context as well (think
of locker numbers, internal snail mail post boxes, etc.) and are
"inherited" from the predecessor. Therefore the amount of numbers is to be
kept as small as possible, otherwise it would increase indefinitely over
time. So the "free" numbers are like a "pool" of numbers, and the
currently smallest one shall be selected for every new staff member.
I should have made this clearer beforehand.

Thanks anyway!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:13 AM
P.A.
 
Posts: n/a
Default Re: Finding lowest free key in table

Am 11.07.2007, 01:19 Uhr, schrieb Adam Englander <adamenglander@yahoo.com>:

> anschouwe@yahoo.com wrote:
>> Hello,
>> I have a table with staff member data. For obligatory historic
>> reasons, each staff member has a unique number assigned, which is
>> taken as primary key. For the same historic reasons the list of staff
>> numbers is not complete, i.e. there are gaps inbetween. When a new
>> staff member is introduced, I want to assign the lowest free number to
>> him or her. How can I find the lowest free integer primary key using
>> MySQL?
>> Example:
>> (staff_number, ...some more staff member information left off)
>> 1
>> 2
>> 5
>> 60
>> 61
>> 63
>> The lowest free staff_number is 3. Is it possible to do this entirely
>> in mysql?
>> I searched around a lot, but as I am fairly new to mysql I probably
>> did not search for the right terms, so I could not find enlightenment
>> yet.
>> Thanks a lot for any hint/advice/sample script!
>> Cheers, Pascal.
>> --
>> Das Bestreben, den Menschen gefällig zu sein,
>> lässt jede geistige Blüte verwelken.
>> ("Baudolino", Umberto Eco)
>>

> There may be a more efficient way that someone else can come up with but
> this will work:
>
> select min(a.incid) as newid from (select id+1 as incid from test) a
> where a.incid not in (select id from test);
>
> "test" is the table holding the staff number and id is the staff number.
>
> Adam


It is great, it works! Thanks a lot! :-)))

Cheers, Pascal.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:35 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com