Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 1st, 2004, 02:57 PM
Registered User
Join Date: Aug 2004
Location: Vantaa, Espoo, Finland.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Mysql auto_increment reset

Hi everybody!
I use MySQL database and PHP, when i delete rows from table with auto_incremet primary key, if i dlete rows with id=1, id=2, id=3, id=4,id=5 the auto_increment point to 6. i want it point to 1.
second problem.
a table with id(s) like this: 1,2,5,6,8,11,24.
i want to fill first smallest free id like 3, then fill 4,7,9,10 and so ..
any mysql syntax?
i have done it with php script but slow and get time out 30 s.
i configured php.ini to 3000s to execute the removing to the end.
please help i will appreciate it.
Reply With Quote
  #2 (permalink)  
Old August 2nd, 2004, 05:22 PM
Friend of Wrox
Join Date: Jun 2003
Location: , , USA.
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo

why do you want to have it from the 1? well, if you want to reset index values, you have to use this sql query:

TRUNCATE table tbl_name

this command will delete everything from the table and start again from 1.... DELETE just does the same but keeps the current auto_increment value....

to solve this problem, you have to use someting more advanced, however i don't see any problems with keys not set to 1 after a delete :D

www.campusgrind.com the college portal
Reply With Quote
The Following User Says Thank You to Moharo For This Useful Post:
  #3 (permalink)  
Old August 3rd, 2004, 07:22 AM
Authorized User
Join Date: Jun 2004
Location: Oslo, , Norway.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts

an auto increment primary key is incremented by one for every record you add into the table, regardless of whether or not there are gaps in the numbering (ie, when you have records 1, 2, 4, 6, 7 and insert a new records, it's PK number is 8).
i'm having a hard time figuring out why you'd want to give new records the same primary key as old deleted ones, but i guess you could get it to work if you wrote a function that checked to see what numbers were missing from the primary key column in the table, and then gave the lowest number to the function that inserts a record into a table giving the record the primary key that you earlier found to not be in use.

do note though that doing this would basicly negate the function of using auto incremented primary keys as you'd be using old ones instead of fresh unused ones, and it could do weird things to the visual output of a relational database if not ALL references in ALL tables to the old primary keys were deleted at the same time the records themselves with those primary keys are deleted.

God, grant me the serenity to accept the things I cannot change, the courage to change the things I can, and the wisdom to hide the bodies of those people I had to kill because they pissed me off.
Reply With Quote

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 Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL script auto_increment vickr1z MySQL 3 January 22nd, 2007 08:22 AM
how 2 reset a form kanoorani Javascript How-To 6 October 10th, 2005 05:45 AM
MySQL password reset tdewitt274 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 August 10th, 2004 06:24 AM
Make an existing field auto_increment Mitch MySQL 1 April 6th, 2004 04:08 AM
MySQL AUTO_INCREMENT field fcamilo Beginning PHP 1 August 21st, 2003 06:29 PM

All times are GMT -4. The time now is 09:53 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.