If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Auto increment Column out of sync

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-14-10, 07:47
narayanyr narayanyr is offline
Registered User
 
Join Date: Apr 2010
Posts: 30
Auto increment Column out of sync

Hi All,
I have an auto increment column defined with data in it,the problem I face is I deleted data for some rows in it and realize that the order has been thrown out of sync.1,2,3,4,5 is now 1,3,4,5.Is there a way to reset the order to 1,2,3,4?

Thanks
Reply With Quote
  #2 (permalink)  
Old 10-14-10, 09:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by narayanyr View Post
Is there a way to reset the order to 1,2,3,4?
yes, but don't even think about it

the purpose of an auto_increment column is to provide unique values, not consecutive values

leave the gaps alone
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-14-10, 10:14
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Hi,

I'm not sure why you need to have this in sync? Also you have to consider that if this is a primary key on a table that any other tables using these referential identifiers will also need to be modified.

To answer your question below you can always drop the field and recreate it as follows:

Code:
SET INSERT_ID=1;
ALTER TABLE table_name DROP COLUMN id;
ALTER TABLE table_name ADD COLUMN id INT UNSIGNED AUTO_INCREMENT
PRIMARY KEY FIRST;
I do remember seeing some code once to get the sequence number of a row in the table and this could be used to update the table too.

The result of these commands can be seen next:

Code:
mysql> select * from test2;
+------+-------+-------+
| id   | name  | count |
+------+-------+-------+
|    1 | Mike  |     0 | 
|    2 | Duke  |     2 | 
|    3 | Smith |     1 | 
|    4 | Dave  |     6 | 
|    6 | Rozie |     8 | 
|    7 | Romeo |     0 | 
|    8 | Khan  |     1 | 
+------+-------+-------+
7 rows in set (0.01 sec)
mysql> set insert_id = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> alter table test2 drop column id;
Query OK, 7 rows affected (0.06 sec)
Records: 7  Duplicates: 0  Warnings: 0
mysql> alter table test2 add column id int unsigned auto_increment primary key first;
Query OK, 7 rows affected (0.03 sec)
Records: 7  Duplicates: 0  Warnings: 0
mysql> select * from test2;
+----+-------+-------+
| id | name  | count |
+----+-------+-------+
|  1 | Mike  |     0 | 
|  2 | Duke  |     2 | 
|  3 | Smith |     1 | 
|  4 | Dave  |     6 | 
|  5 | Rozie |     8 | 
|  6 | Romeo |     0 | 
|  7 | Khan  |     1 | 
+----+-------+-------+
7 rows in set (0.00 sec)
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #4 (permalink)  
Old 10-14-10, 10:43
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
don't use an auto number value as a surrogate for a value that has meaning in the outside world

if you must have sequential numbers then write your own routine to allow for sequential numbers or persuade your beancounters that its not neccessary.. they will disagree.

autonumbers are for internal use, use within the system to make a value unique, they should not have meaning applied to them outside the system for things like Order, Invoice or GRN's.

Im certain Ronans a suggestion above works, but that is curing the symptoms NOT the problem.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 10-14-10, 12:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
and of course if there are any foreign keys which reference this auto_increment, then renumbering the values will create an ~unmitigated~ disaster
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 10-15-10, 05:41
narayanyr narayanyr is offline
Registered User
 
Join Date: Apr 2010
Posts: 30
Great Thanks guys
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On