Welcome Guest ( Log In | Register)



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Can Reset The Id Auto Increment?, phpMyadmin
badinfluence
post Sep 7 2005, 09:54 PM
Post #1


Super Member
*********

Group: Members
Posts: 283
Joined: 10-October 04
Member No.: 1,637



hi,
Can we reset the id auto increment in the mysql database using by phpMyadmin?
let's say the shoutbox. the id #number table.. somehow i deleted some rows.. and i wanted to re-order/reset the id number acendingly in order rather than skip out..
thks

Go to the top of the page
 
+Quote Post
SystemWisdom
post Sep 7 2005, 11:44 PM
Post #2


Advanced Member
*******

Group: Members
Posts: 117
Joined: 3-May 05
From: A Canadian South of the 49th Parallel
Member No.: 6,544



I don't think you can do that using phpMyAdmin, or even SQL for that matter, but you could write a PHP script to do that for ya...
Make a query like:

Select ID from <table> order by ID

then use a while/for loop to iterate through the results, and update each row with a new number for the ID.. you could use a PHP variable to track the current ID number, and increment it with each loop...

Update <table> set ID=???

Should work.. Although I have never tried manually updating an auto_increment field.. come to think of it, it seems pointless to adjust it, since it is dealt with internally anyway, and the actual value should never affect your script...

If that doesn't work, and you still need to sort them, try adding another field to the DB table, like a sort-order field..
Go to the top of the page
 
+Quote Post
tigen28
post Sep 9 2005, 07:31 AM
Post #3


Member [Level 1]
****

Group: Members
Posts: 69
Joined: 6-August 05
Member No.: 10,375



Please be warn that this is the manual-intensive-labor way, but no thinking require.

If you want to change any field in the table and do not mind changing only 1 field at a time, you can always go to php admin.

Then, select your database.

Next, click on the "table" you want to modify.

Last find the field and click edit; so basicially, click on field of a time and edit.

Hopefully, you don't have much entries in that table; otherwise, I highly unrecommend this route.

Well, hope it helps.
Go to the top of the page
 
+Quote Post
badinfluence
post Sep 14 2005, 02:51 AM
Post #4


Super Member
*********

Group: Members
Posts: 283
Joined: 10-October 04
Member No.: 1,637



QUOTE(SystemWisdom @ Sep 8 2005, 08:44 AM)
I don't think you can do that using phpMyAdmin, or even SQL for that matter, but you could write a PHP script to do that for ya...
Make a query like:
Select ID from <table> order by ID
then use a while/for loop to iterate through the results, and update each row with a new number for the ID..  you could use a PHP variable to track the current ID number, and increment it with each loop...
Update <table> set ID=???
*



yes, System that's perfect and exactly true. Thanks for that trick. Indeed a littol risky humm.. phpMyAdmin should do that function by default. See whether i can request the feature about that on their sourceforge site.

this is the old and null way tongue.gif backup and re-construct laugh.gif

Thanks System and tigen for discussing this,

Go to the top of the page
 
+Quote Post
beeseven
post Sep 22 2005, 11:02 PM
Post #5


Privileged Member
*********

Group: Members
Posts: 629
Joined: 26-February 05
Member No.: 3,995



I asked about this a while ago. A quick and easy way to set the next ID is this:
CODE
ALTER TABLE [tablename] AUTO_INCREMENT = [number]

where [tablename] is the name of the table and [number] is the next ID. I'm not sure of a quick way to change other, existing IDs, though.
Go to the top of the page
 
+Quote Post
p_a
post Sep 24 2005, 10:41 AM
Post #6


Newbie [Level 1]
*

Group: Members
Posts: 22
Joined: 23-September 05
Member No.: 12,187



It would be better not to do reorder of ID fields anyway. I do not want even to show them in my forms, grids. Maybe the new numbered field would be the best way to solve it.

Why shoud everybody avoid changing ID fields? Simply because of possibility of having relations in database between tables. ID fields sometimes have their main role in relations and playing games with them will make a mess. I would never do it.

Go to the top of the page
 
+Quote Post
csmith
post Sep 25 2005, 06:24 AM
Post #7


Newbie [Level 1]
*

Group: Members
Posts: 11
Joined: 25-September 05
Member No.: 12,225



This is a pretty primitive solution, but after you delete the rows which you wish to remove from the table, you could export the remaining rows into either a .csv file. In this subsequent excel file you could remove the first column which contains the id, save this file, and then re-import this back into your database table (you should click on the option of over-writing rows in this table which have the same id/primary key value as a row in the excel file), or re-import into a new table. The ids will be automatically re-set without any numbers skipped - it should not take too long, plus you should not lose any data in the process
Go to the top of the page
 
+Quote Post
sobhan
post Oct 2 2005, 02:36 PM
Post #8


Newbie [Level 1]
*

Group: Members
Posts: 21
Joined: 1-October 05
Member No.: 12,412



I don't think you can reset, you can only change value in phpMyAdmin.
Go to the top of the page
 
+Quote Post
najib
post May 10 2006, 04:18 AM
Post #9


Newbie
*

Group: Members
Posts: 2
Joined: 10-May 06
Member No.: 23,367



I know it's a zombie post, but I thought I'd give my part nonetheless.

CODE

ALTER TABLE `tablenamegoeshere` AUTO_INCREMENT =0


The above will reset the auto_increment to the lowest value. So everytime an entry is removed, drop any remaing ID's that are larger than the current entry by one, then query the database with the exact line above and you wont have any problems.

Sweet! ph34r.gif
Go to the top of the page
 
+Quote Post
iGuest
</