MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 6th, 2012, 07:02 PM
leathan leathan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 27 leathan User rank is Private First Class (20 - 50 Reputation Level)leathan User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 25 m 15 sec
Reputation Power: 0
MySQL PRIMARY KEY Question

Hi, I have a question about the behavior of the PRIMARY KEY in MySQL.

Lets say I have a database with 10 entries the primary keys are 1-10 respectively.

Lets say I then delete the entry with a primary key of 7.

The next time I enter an entry it gets assigned a primary key of 11. Is there anyway to make the database realize that the primary key 7 is the next primary key that should be assigned? Or would I have to make some kind of script that runs through the table to find gaps in the primary key.

It would be really nice if the database could somehow set the next primary key to 7 (or whatever entry was deleted) and not 11.

Thanks in advance!

Reply With Quote
  #2  
Old October 6th, 2012, 07:56 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,681 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 2 h 20 m 36 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Don't do it. AUTO_INCREMENT primary keys are not meant to be entirely consecutive and sequential. Let them alone, holes and all.

If you need something sequential then that's a different problem.

Last edited by requinix : October 6th, 2012 at 07:59 PM.

Reply With Quote
  #3  
Old October 6th, 2012, 08:02 PM
leathan leathan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 27 leathan User rank is Private First Class (20 - 50 Reputation Level)leathan User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 25 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by requinix
Don't do it. AUTO_INCREMENT primary keys are not meant to be entirely consecutive and sequential. Let them alone, holes and all.

If you need something sequential then that's a different problem.


It would just make my script alot easier to write if there were no holes in the primary keys.

Reply With Quote
  #4  
Old October 6th, 2012, 09:18 PM
Guelphdad's Avatar
Guelphdad Guelphdad is offline
Hockey face
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: St. Catharines, Canada
Posts: 8,141 Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 34 m 13 sec
Reputation Power: 1315
no it wouldn't really. your script should get the next number from the database, not rely on the numbers being consecutive.

Reply With Quote
  #5  
Old October 7th, 2012, 10:27 AM
leathan leathan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 27 leathan User rank is Private First Class (20 - 50 Reputation Level)leathan User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 25 m 15 sec
Reputation Power: 0
Ok, I figured it wasnt possible, thanks for the replies guys, and your correct there is other ways i could make my script.

Thanks again, much appreciated

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > MySQL PRIMARY KEY Question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap