Web Design Forum: MySQL auto-increment fields - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

MySQL auto-increment fields Rate Topic: -----

#1 User is offline   Sam G 

  • Forum Newcomer
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,860
  • Joined: 06-March 09
  • Reputation: 54
  • Gender:Male
  • Location:Dreamland
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 02 August 2009 - 08:05 AM

OK say I have 3 rows, with a column called "ID" as follows:


id  other_column
---------------------
1   text
2   text
3   text



If I delete the third row, is there any way I can tell MySQL to use an ID of "3" when a new row is inserted, rather than "4"?
0

#2 User is offline   pat24 

  • Guru of nothing important!!
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,183
  • Joined: 29-April 08
  • Reputation: 131
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 02 August 2009 - 10:47 AM

View PostSam G, on 02 August 2009 - 08:05 AM, said:

OK say I have 3 rows, with a column called "ID" as follows:

 
 id  other_column
 ---------------------
 1   text
 2   text
 3   text
 
 


If I delete the third row, is there any way I can tell MySQL to use an ID of "3" when a new row is inserted, rather than "4"?


I am sure there will be a piece of code somewhere to do this. But why would you want to, if the table has a relationship throughout the database then you would have major problems. It could seriously screw the database.

Pat
0

#3 User is offline   ElanMan 

  • In, out, shake it all about...
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 3,298
  • Joined: 11-March 08
  • Reputation: 54
  • Gender:Male
  • Location:Darlington
  • Experience:Nothing
  • Area of Expertise:Nothing

Posted 02 August 2009 - 12:10 PM

As Pat said, unless there is very good reason to do this, don't do it.
However, if you want to, use the ALTER TABLE statement like this:
ALTER TABLE tablename DROP id, ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST;

Basically dropping the column and adding it back in the same statement. FIRST places the column first in the table, where it was originally.
0

#4 User is offline   Sam G 

  • Forum Newcomer
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,860
  • Joined: 06-March 09
  • Reputation: 54
  • Gender:Male
  • Location:Dreamland
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 02 August 2009 - 12:13 PM

It's just something that I'm working on locally, not something I'd need to do on a live production system.

So there's no way around it without dropping the table first?
0

#5 User is offline   ElanMan 

  • In, out, shake it all about...
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 3,298
  • Joined: 11-March 08
  • Reputation: 54
  • Gender:Male
  • Location:Darlington
  • Experience:Nothing
  • Area of Expertise:Nothing

Posted 02 August 2009 - 12:14 PM

You're not dropping the table, just the id column
0

#6 User is offline   Sam G 

  • Forum Newcomer
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,860
  • Joined: 06-March 09
  • Reputation: 54
  • Gender:Male
  • Location:Dreamland
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 02 August 2009 - 12:19 PM

Sorry I meant column (half asleep today...)

I just found this code snippet on the web (probably should have done that first before asking on here but never mind!):

Quote

Problem One:
If I have entered 10 records, and deleted 9th, 10th records. The next auto increment value will be 11, not 9.

Solution:
Run a query: ALTER TABLE tablename AUTO_INCREMENT = 1

This will reset the next auto increment value to current largest value in the auto increment column + 1. So, the auto increment value of next inserted record will start from 9.


Source: http://www.liewcf.co...reament-number/

Would this not be a better solution?
0

#7 User is offline   ElanMan 

  • In, out, shake it all about...
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 3,298
  • Joined: 11-March 08
  • Reputation: 54
  • Gender:Male
  • Location:Darlington
  • Experience:Nothing
  • Area of Expertise:Nothing

Posted 02 August 2009 - 07:13 PM

Sorry, I must have also been asleep :)
I didn't read your post correctly.
I was thinking of re-ordering the auto-incremented id to remove 'gaps' in the whole table, not for the next insert.
Sorry about that :)
0

#8 User is offline   Sam G 

  • Forum Newcomer
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,860
  • Joined: 06-March 09
  • Reputation: 54
  • Gender:Male
  • Location:Dreamland
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 02 August 2009 - 09:31 PM

LOL no worries :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users